天天看点

Android Launcher3修改应用图标,隐藏应用图标

Launcher运行时,会执行LauncherModel的loadAllApps方法,加载所有应用的详细信息。这时候,如果你想过

滤掉你不想显示的应用,你可以修改源码,把应用过滤掉。

我这里的示列是Launcher第一次加载时,会把Android的键盘应用添加到主页面。所以我需要把该应用隐藏,不

显示在Launcher桌面。这里贴LoadAllApps的部分代码。

// Create the ApplicationInfos
for (int i = 0; i < apps.size(); i++) {
    LauncherActivityInfoCompat app = apps.get(i);
    // This builds the icon bitmaps.
    AppInfo appInfo = new AppInfo(mContext, app, user, mIconCache, mLabelCache);
//  Log.e(TAG, "packagename: "+appInfo.componentName.getPackageName());
    //隐藏AndroidAOSP加载到桌面
    if (!appInfo.componentName.getPackageName().equals("com.android.inputmethod.latin")){
        mBgAllAppsList.add(appInfo);
//  Log.e(TAG, "packagename222: "+appInfo.componentName.getPackageName());
    }else{
        //过滤掉该APP,不显示在Launcher界面中
    }
}
           

通过追踪代码,你会发现,控制APP应用图标的代码在IconCache的cacheLocked方法中。当然,你也可以在其他地方修改。这个没有限制。判断应用的包名和你应用的需要修改的一致,这时候就可以修改应用的包名了

/**
 * Retrieves the entry from the cache. If the entry is not present, it creates a new entry.
 * This method is not thread safe, it must be called from a synchronized method.
 */
private CacheEntry cacheLocked(ComponentName componentName, LauncherActivityInfoCompat info,
        HashMap<Object, CharSequence> labelCache, UserHandleCompat user, boolean usePackageIcon) {
    CacheKey cacheKey = new CacheKey(componentName, user);
    CacheEntry entry = mCache.get(cacheKey);
    if (entry == null) {
        entry = new CacheEntry();
        mCache.put(cacheKey, entry);

        if (info != null) {
            ComponentName labelKey = info.getComponentName();
            if (labelCache != null && labelCache.containsKey(labelKey)) {
                entry.title = labelCache.get(labelKey).toString();
            } else {
                entry.title = info.getLabel().toString();
                if (labelCache != null) {
                    labelCache.put(labelKey, entry.title);
                }
            }

            entry.contentDescription = mUserManager.getBadgedLabelForUser(entry.title, user);
            entry.icon = Utilities.createIconBitmap(
                    info.getBadgedIcon(mIconDpi), mContext);

            Log.d(TAG, "PackageName: "+componentName.getPackageName());
            
            
            /**
             * 这里修改图标
             */
            //设置
            if (componentName.getPackageName().equals("com.android.settings")) {

                Bitmap bitmap = BitmapFactory.decodeResource(mContext.getResources(),
                        R.mipmap.system_setting);
                entry.icon = bitmap;
            }