天天看點

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;
            }