android 怎样修改系统launcher中应用的图标

有些已经封装好的apk,怎样替换图标,这个需求好像有点奇怪。话不多说,直接上代码。在AllAppsList.java有个add方法,会把所有安装的apk信息都保存到一个ArrayList里面

 public void add(AppInfo info) {
        if (mAppFilter != null && !mAppFilter.shouldShowApp(info.componentName)) {
            return;
        }
        if (findActivity(data, info.componentName, info.user)) {
            LauncherLog.d(TAG, "Application " + info + " already exists in app list, app = "
                + info);
            return;
			
        }
		LauncherLog.d(TAG, "add componentName "+info.componentName);
        if(info.componentName.getPackageName().equals("com.android.music")){
			LauncherLog.d(TAG, "add componentName equals");
			info.iconBitmap = BitmapFactory.decodeStream(getClass().getResourceAsStream("/res/drawable/ic_setting.png"));
		}
		
        data.add(info);
        added.add(info);
    }
在这个方法里面添加一个判断,如果是某个包名,就把该包名的icon的信息替换为自己想添加的图片。修改完后,在launcher的第二层界面生效了,但是拖动图标生成桌面快捷方式的图标还是原来的图标。接着跟代码,在ShortcutInfo.java类的构造方法里面过滤,这样通过拖动图标生成桌面快捷方式的图标修改成功。

/** TODO: Remove this.  It's only called by ApplicationInfo.makeShortcut. */
    public ShortcutInfo(AppInfo info) {
        super(info);
        title = Utilities.trim(info.title);
        intent = new Intent(info.intent);
        customIcon = false;
        flags = info.flags;
        isDisabled = info.isDisabled;
		if(info.componentName.getPackageName().equals("com.android.music")){
			Log.d(TAG, "ShortcutInfo equals");
			mIcon = BitmapFactory.decodeStream(getClass().getResourceAsStream("/res/drawable/ic_setting.png"));
		}
		Log.d(TAG,"ShortcutInfo3",new RuntimeException("test").fillInStackTrace());
    }
还有一个问题,默认的default_workspace中加载的图片还是没有改变。在LauncherModel.java类里面有个getAppShortcutInfo方法,在这面加一个过滤。

if(componentName.getPackageName().equals("com.android.music")){			
			Log.d(TAG, "ShortcutInfo equals");
			info.setIcon(BitmapFactory.decodeStream(getClass().getResourceAsStream("/res/drawable/ic_setting.png")));
		}
这样就完全把一个app的icon替换成任意的一张图片。





猜你喜欢

转载自blog.csdn.net/qq_32072451/article/details/79152584