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

2023-09-11 10:47:23 作者:凉城无爱

是否有可能直接从程序更改应用程序图标? 我的意思是,变化的icon.png 水库\绘制文件夹。 我想,让用户改变从程序,以便下一次应用程序的图标,他们将看到的previously选择图标启动程序。

Is it possible to change an application icon directly from the program? I mean, change icon.png in the res\drawable 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.

推荐答案

这是一个老问题,但依然活跃,并没有明确的Andr​​oid功能。而来自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.

下面是code。它有一个按钮增量。当pressed,快捷键被替换为一个具有新的计数值。

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

注:

这样的作品也,如果你的应用程序控制主屏幕,如更多的快捷键不同的额外的在意图。他们只是需要不同的名称,以便正确的卸载和重新安装。

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"

启动程序写入吐司时,快捷方式installad和一个当一个快捷方式卸载。所以,我得到两个吐司取值我每次更改图标。这不是完美的,但很好,只要我的应用程序的其余部分是完美的......

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...