如何在 Android 中以编程方式更改应用程序图标?

如何在 Android 中以编程方式更改应用程序图标?

问题描述:

是否可以直接从程序中更改应用程序图标?
我的意思是,更改 resdrawable 文件夹中的 icon.png.
我想让用户从程序中更改应用程序的图标,以便下次他们可以在启动器中看到之前选择的图标.

Is it possible to change an application icon directly from the program?
I mean, change icon.png in the resdrawable folder.
I would like to let users to change application's icon from the program so next time they would see the previously selected icon in the launcher.

这是一个老问题,但仍然有效,因为没有明确的 Android 功能.来自 facebook 的人找到了解决办法——不知何故.今天,我找到了一种适合我的方法.不完美(请参阅此答案末尾的备注)但它有效!

It's an old question, but still active as there is no explicit Android feature. And the guys from facebook found a work around - somehow. Today, I found a way that works for me. Not perfect (see remarks at the end of this answer) but it works!

主要想法是,我更新由启动器在主屏幕上创建的应用程序快捷方式的图标.当我想更改快捷方式图标上的某些内容时,我会先将其删除,然后使用新位图重新创建.

Main idea is, that I update the icon of my app's shortcut, created by the launcher on my home screen. When I want to change something on the shortcut-icon, I remove it first and recreate it with a new bitmap.

这是代码.它有一个按钮increment.按下后,快捷方式将替换为具有新计数的快捷方式.

Here is the code. It has a button increment. When pressed, the shortcut is replaced with one that has a new counting number.

首先,您的清单中需要这两个权限:

First you need these two permissions in your manifest:

<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
<uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" />

那你就需要这两种安装和卸载快捷方式的方法了.shortcutAdd 方法创建一个带有数字的位图.这只是为了证明它确实发生了变化.您可能想在应用中更改该部分.

Then you need this two methods for installing and uninstalling shortcuts. The shortcutAdd method creates a bitmap with a number in it. This is just to demonstrate that it actually changes. You probably want to change that part with something, you want in your app.

private void shortcutAdd(String name, int number) {
    // Intent to be send, when shortcut is pressed by user ("launched")
    Intent shortcutIntent = new Intent(getApplicationContext(), Play.class);
    shortcutIntent.setAction(Constants.ACTION_PLAY);

    // Create bitmap with number in it -> very default. You probably want to give it a more stylish look
    Bitmap bitmap = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);
    Paint paint = new Paint();
    paint.setColor(0xFF808080); // gray
    paint.setTextAlign(Paint.Align.CENTER);
    paint.setTextSize(50);
    new Canvas(bitmap).drawText(""+number, 50, 50, paint);
    ((ImageView) findViewById(R.id.icon)).setImageBitmap(bitmap);

    // Decorate the shortcut
    Intent addIntent = new Intent();
    addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
    addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, name);
    addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON, bitmap);

    // Inform launcher to create shortcut
    addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
    getApplicationContext().sendBroadcast(addIntent);
}

private void shortcutDel(String name) {
    // Intent to be send, when shortcut is pressed by user ("launched")
    Intent shortcutIntent = new Intent(getApplicationContext(), Play.class);
    shortcutIntent.setAction(Constants.ACTION_PLAY);

    // Decorate the shortcut
    Intent delIntent = new Intent();
    delIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
    delIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, name);

    // Inform launcher to remove shortcut
    delIntent.setAction("com.android.launcher.action.UNINSTALL_SHORTCUT");
    getApplicationContext().sendBroadcast(delIntent);
}

最后,这里有两个监听器来添加第一个快捷方式并使用递增计数器更新快捷方式.

And finally, here are two listener to add the first shortcut and update the shortcut with an incrementing counter.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.test);
    findViewById(R.id.add).setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            shortcutAdd("changeIt!", count);
        }
    });
    findViewById(R.id.increment).setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            shortcutDel("changeIt!");
            count++;
            shortcutAdd("changeIt!", count);
        }
    });
}

备注:

  • 如果您的应用程序控制主屏幕上的更多快捷方式,这种方式也适用,例如在 Intent 中有不同的额外内容.他们只需要不同的名称,以便卸载并重新安装正确的名称.

  • This way works also if your App controls more shortcuts on the home screen, e.g. with different extra's in the Intent. They just need different names so that the right one is uninstalled and reinstalled.

Android 中快捷方式的编程处理是众所周知的、广泛使用但未得到官方支持的 Android 功能.它似乎适用于默认启动器,我从未在其他任何地方尝试过.所以不要怪我,当你收到这个用户的电子邮件它不适用于我的 XYZ,双根,超级爆炸的手机"

The programmatical handling of shortcuts in Android is a well known, widely used but not officially supported Android feature. It seems to work on the default launcher and I never tried it anywhere else. So dont blame me, when you get this user-emails "It does not work on my XYZ, double rooted, super blasted phone"

启动器在安装快捷方式时写入Toast,卸载快捷方式时写入一个Toast.因此,每次更改图标时,我都会收到两个 Toast.这并不完美,但只要我的应用程序的其余部分是完美的......

The launcher writes a Toast when a shortcut was installad and one when a shortcut was uninstalled. So I get two Toasts every time I change the icon. This is not perfect, but well, as long as the rest of my app is perfect...