如何在Android Launcher中隐藏另一个应用程序图标(可见性),如Launcher Pro?

时间:2019-03-12 00:35:43

标签: android icons android-manifest launcher android-icons

对不起,编码员,如果我的帖子与另一个有关隐藏图标启动器的问题类似。我有一个最终项目要制作一个可以隐藏另一个应用程序启动器图标的应用程序。
首先,我得到了如下代码: Manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.qp.dev.parentalcontrol">

<uses-feature
    android:name="android.hardware.touchscreen"
    android:required="false" />

<uses-permission android:name="android.permission.CHANGE_COMPONENT_ENABLED_STATE"
    tools:ignore="ProtectedPermissions"
    android:protectionLevel="signature"/>

<uses-sdk
    android:minSdkVersion="27"
    android:targetSdkVersion="27" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

显示我的应用程序列表的

.java和隐藏其他应用程序图标启动器的所有者 AppListAdapter.java

public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;
    LayoutInflater inflater = ((Activity) context).getLayoutInflater();

    if (convertView == null) {
        convertView = inflater.inflate( R.layout.apk_list, parent, false );
        holder = new ViewHolder();

        holder.apkName = (TextView) convertView.findViewById( R.id.appname );
        holder.chboxApk = (CheckBox) convertView.findViewById( R.id.checked_tv );

        convertView.setTag( holder );
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    final PackageInfo packageInfo = (PackageInfo) getItem( position );
    Drawable appIcon = packageManager.getApplicationIcon( packageInfo.applicationInfo );
    String apkName = packageManager.getApplicationLabel( packageInfo.applicationInfo ).toString();

    appIcon.setBounds( 0, 0, 80, 80 );
    holder.apkName.setCompoundDrawables( appIcon, null, null, null );
    holder.apkName.setCompoundDrawablePadding( 15 );
    holder.apkName.setText( apkName );

    final int adapterPosition = position;
    final ViewGroup newParent = parent;

    PackageInfo packageInfoPref = (PackageInfo) getItem( adapterPosition );
    final String namaPackagePref = packageInfoPref.applicationInfo.packageName;

    // buka informasi aplikasi
    holder.apkName.setOnClickListener( new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        PackageInfo packageInfo = (PackageInfo) getItem( adapterPosition );

        Intent appInfo = new Intent( context.getApplicationContext(), ApkInfo.class );

        String namaPackage = packageInfo.applicationInfo.packageName;
        String appName = "";
        int versi = 0;
        String path = "";
        int feature = 0;
        String waktuInstal = "";
        String lastModify = "";

        try {
            appName = packageManager.getApplicationInfo( namaPackage, 0 ).name;
            versi = packageManager.getApplicationInfo( namaPackage, 0 ).targetSdkVersion;
            path = packageManager.getApplicationInfo( namaPackage, 0 ).sourceDir;
            waktuInstal = setDateFormat( packageInfo.firstInstallTime );
            lastModify = setDateFormat( packageInfo.lastUpdateTime );

        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }

        Toast.makeText( context, "Nama Package: " + namaPackage
                        + "\n\n Nama aplikasi: " + appName
                        + "\n\n Versi : " + versi
                        + "\n\n Path : " + path
                        + "\n\n Install : " + waktuInstal
                        + "\n\n Modify : " + lastModify
                , Toast.LENGTH_SHORT ).show();

        appInfo.putExtra( "namaPackage1", namaPackage );
        appInfo.putExtra( "appName1", appName );
        appInfo.putExtra( "versi1", versi );
        appInfo.putExtra( "path1", path );
        appInfo.putExtra( "waktuInstal1", waktuInstal );
        appInfo.putExtra( "lastModify1", lastModify );
        context.startActivity( appInfo );
        }
    } );

    //checkBox
    final ViewHolder viewHolder = new ViewHolder();

    PackageManager pakcageNamePrefValue = context.getPackageManager();
    SharedPreferences prefs = context.getSharedPreferences(namaPackagePref, 0);
    String restoredText = prefs.getString("status", null);
    if (restoredText != null) {
        int status = prefs.getInt("status", 0); //0 is the default value.

        if(status==0){
            holder.chboxApk.setChecked( false );
        }else if(status==1){
            holder.chboxApk.setChecked( true );
        }
    }

    try{
        if(holder.chboxApk.isChecked()){
            PackageManager p = packageManager;
            Intent launchIntent = context.getPackageManager().getLaunchIntentForPackage( namaPackagePref );
            ComponentName componentName = new ComponentName(namaPackagePref,launchIntent.getComponent().getClassName()); // activity which is first time open in manifiest file which is declare as <category android:name="android.intent.category.LAUNCHER" />
            p.setComponentEnabledSetting( componentName,PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP );
        }else{
            PackageManager p = packageManager;
            Intent launchIntent = context.getPackageManager().getLaunchIntentForPackage( namaPackagePref );
            ComponentName componentName = new ComponentName(namaPackagePref,launchIntent.getComponent().getClassName()); // activity which is first time open in manifiest file which is declare as <category android:name="android.intent.category.LAUNCHER" />
            p.setComponentEnabledSetting( componentName,PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP );
        }
    }catch (Exception e){
        e.printStackTrace();
    }

    holder.chboxApk.setOnClickListener( new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Intent launchIntent = context.getPackageManager().getLaunchIntentForPackage( namaPackagePref );
            Toast.makeText( context, "nama: "+namaPackagePref, Toast.LENGTH_SHORT ).show();
            Toast.makeText( context, "" + Objects.requireNonNull( Objects.requireNonNull( launchIntent ).getComponent() ).getClassName(), Toast.LENGTH_SHORT ).show();

            boolean isChecked = true;
            PackageManager pakcageNamePrefValue = context.getPackageManager();
            SharedPreferences.Editor editor = context.getSharedPreferences(namaPackagePref, 0).edit();

            if(isChecked){

                PackageManager p = packageManager;
                ComponentName componentName = new ComponentName(namaPackagePref, Objects.requireNonNull( launchIntent.getComponent() ).getClassName()); // activity which is first time open in manifiest file which is declare as <category android:name="android.intent.category.LAUNCHER" />
                p.setComponentEnabledSetting( componentName,PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP );
                editor.putInt("status", 1);
                editor.apply();

                Toast.makeText( context, "checked", Toast.LENGTH_SHORT ).show();
            }
            else {
                PackageManager p = packageManager;
                ComponentName componentName = new ComponentName(namaPackagePref,launchIntent.getComponent().getClassName()); // activity which is first time open in manifiest file which is declare as <category android:name="android.intent.category.LAUNCHER" />
                p.setComponentEnabledSetting( componentName,PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP );
                editor.putInt("status", 0);
                editor.apply();

            }
        }
    } );

    return convertView;
}

我要隐藏另一个应用启动器图标(设置图标的可见性)。我尝试了代码

PackageManager.COMPONENT_ENABLED_STATE_DISABLED

禁用/隐藏应用程序和

PackageManager.COMPONENT_ENABLED_STATE_ENABLED

以启用/显示图标应用程序。
这是应用程序列表的显示: enter image description here

当我单击复选框隐藏器时有2种不同的错误
1.当我检查system-app时,出现此错误:
enter image description here

  

at java.util.Objects.requireNonNull(Objects.java:203)   
at com.qp.dev.parentalcontrol.adapter.AppAdapter $ 2.onClick(AppAdapter.java:208)


2.当我检查非系统应用程序时,出现此错误:
enter image description here

  android.os.Parcel.readException上的

(Parcel.java:2004)   
at android.os.Parcel.readException(Parcel.java:1950)   
在android.content.pm.IPackageManager $ Stub $ Proxy.setComponentEnabledSetting(IPackageManager.java:4732)   
在android.app.ApplicationPackageManager.setComponentEnabledSetting(ApplicationPackageManager.java:2276)   
at com.qp.dev.parentalcontrol.adapter.AppAdapter $ 2.onClick(AppAdapter.java:221)

我打算通过制作此家长控制应用来完成我的最后一个项目,以取得学士学位。我希望有一种解决该程序的方法,因为我尝试了一个类似Nova Launcher的应用程序,它可以隐藏另一个天堂之类的应用程序。 谢谢您的关注。

0 个答案:

没有答案