Android Launcher隐藏指定应用的启动图标

我们常常在工作中遇到这样一个需求,要在Launcher中隐藏某个应用的启动图标(如预装的输入法、动态壁纸等等)。完成这个需求,首先要了解Launcher加载应用启动图标的过程。以Android4.4为例,Launcher3加载应用图标在LauncherModel.java中完成,来看LauncherModel.java中loadAllApps()方法的一段代码:

            final PackageManager packageManager = mContext.getPackageManager();
            final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
            mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);

            // Clear the list of apps
            mBgAllAppsList.clear();

            // Query for the set of apps
            final long qiaTime = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0;
            List<ResolveInfo> apps = packageManager.queryIntentActivities(mainIntent, 0);
            ......//省略无关代码

            // Create the ApplicationInfos
            for (int i = 0; i < apps.size(); i++) {
                ResolveInfo app = apps.get(i);
                // This builds the icon bitmaps.
                mBgAllAppsList.add(new AppInfo(packageManager, app,
                        mIconCache, mLabelCache));
            }

这样一看清晰了吧。根据以上代码,我们知道Launcher是通过指定的Intent,更确切的说是intent-filter来加载应用图标的,所以我们可以这样来隐藏图标:

          1、有源代码的应用程序,直接修改AndroidManifest.xml文件,删掉所有Activity中intent-filter的

                <category android:name="android.intent.category.LAUNCHER" />

          2、没有源代码的应用,修改loadAllApps()方法:

             //数组array_hide_app_icon中是所有需要隐藏图标的应用的包名和类名。

            //例如隐藏拨号:com.android.dialer/.DialtactsActivity

 

             String[] hideAppIconArray = mContext.getResources().getStringArray(R.array.array_hide_app_icon);
             // Create the ApplicationInfos
             for (int i = 0; i < apps.size(); i++) {
                 ResolveInfo app = apps.get(i);

                //过滤掉需要隐藏的应用
                StringBuilder sb = new StringBuilder();
                ComponentName componentName = getComponentNameFromResolveInfo(app);
                String shortClassName = componentName.getShortClassName();
                String packageName = componentName.getPackageName();
                String shortComponentName = sb.append(packageName).append('/')
                                              .append(shortClassName).toString();

                boolean needHide = false;
                for (String str : hideAppIconArray ){
                    if (str != null && str.equals(shortComponentName) ){
                        needHide = true;
                    }
                }

                if(needHide){
                    apps.remove(i);
                    continue;
                }

                // This builds the icon bitmaps.
                mBgAllAppsList.add(new AppInfo(packageManager, app,
                         mIconCache, null));

这样修改后我们就可以隐藏指定应用的启动图标了,以后如果有其它需要隐藏的应用直接在arrays.xml的array_hide_app_icon中添加应用的包名和类名即可。

展开阅读全文

没有更多推荐了,返回首页