在安卓系统中如何通过编程改变应用的图标?

177

是否可以直接从程序更改应用程序图标?
我的意思是,在 res\drawable 文件夹中更改 icon.png
我希望让用户能够从程序中更改应用程序的图标,这样下次他们在启动器中看到的将是之前选择的图标。

11个回答

154
请尝试以下方法,对我很有效:
1. 修改你的 AndroidManifest.xml 文件中的 MainActivity 部分,在 intent-filter 部分中删除带有 MAIN 类别的行。
<activity android:name="ru.quickmessage.pa.MainActivity"
    android:configChanges="keyboardHidden|orientation"
    android:screenOrientation="portrait"
    android:label="@string/app_name"
    android:theme="@style/CustomTheme"
    android:launchMode="singleTask">
    <intent-filter>
        ==> <action android:name="android.intent.action.MAIN" /> <== Delete this line
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

2. 为每个图标创建<activity-alias>,例如:

<activity-alias android:label="@string/app_name" 
    android:icon="@drawable/icon" 
    android:name=".MainActivity-Red"
    android:enabled="false"
    android:targetActivity=".MainActivity">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>   
</activity-alias>

3. 编程设置:为适当的activity-alias设置ENABLE属性。

 getPackageManager().setComponentEnabledSetting(
        new ComponentName("ru.quickmessage.pa", "ru.quickmessage.pa.MainActivity-Red"), 
            PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);

注意,至少必须始终启用一个。


4
遗憾的是,这个功能在我尝试过的设备上表现不一。在HTC Desire 2.2 上可以正常运行,但在Galaxy Nexus 4.2.2和Nexus 7 4.3上不可靠。在Galaxy Nexus上,可能会导致应用程序的所有图标消失,还会删除任何小部件。很可惜,所以我不得不在后来的设备上移除这个功能。 - Richard Le Mesurier
11
自从我删除了以下内容:<action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" />,我的应用程序无法启动。 - noobProgrammer
5
应用程序运行错误。找不到默认活动。 - CopsOnRoad
4
应用程序出错。找不到默认活动。 - Kamil Ibadov
5
这个功能很好用,但是“DONT_KILL_APP”标志似乎没有起作用(或者我理解有误)。在图标或活动别名被切换一两秒钟内,应用程序就会被杀死并从后台堆栈中移除。尽管图标已经在启动器中替换了,应用程序却被杀死了 :/有人有什么想法可以防止应用程序被杀死吗? - Adi B
显示剩余16条评论

84

这是一个老问题,因为没有明确的Android功能而仍然活跃。Facebook的人们发现了一个解决方法 - 不知何故。今天,我找到了一种适合我的方法。不完美(请参见本答案末尾的备注),但它起作用!

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

以下是代码。 它有一个名为 increment 的按钮。 按下后,快捷方式将被替换为具有新计数编号的快捷方式。

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

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

那么您需要这两种方法来安装和卸载快捷方式。 shortcutAdd 方法创建一个带有数字的位图。这只是为了演示它确实发生了变化。您可能想使用您应用程序中需要的其他内容来更改该部分。

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

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

@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中具有不同的附加项,则这种方法也适用。它们只需要不同的名称,以便正确的快捷方式被卸载和重新安装。

  • 在Android中编程处理快捷方式是一项众所周知、广泛使用但未经官方支持的Android功能。它似乎在默认启动器上工作,我从未在其他任何地方尝试过。因此,当您收到这样的用户电子邮件“它不能在我的 XYZ 上运行,这是个双重根手机”时,请不要责怪我。

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


12
今天的日历应用每天更换图标,没有弹出通知。 - Jim McKeeth
1
@ Jim(我想)这实际上是一个小部件。 - JacksOnF1re
3
很遗憾,在Marshmallow中_shortcutDel_已经不起作用了,参见https://dev59.com/6U_Ta4cB1Zd3GeqPDsN2#33731620。 - Taifun
3
这将替换快捷图标,而不是启动器图标。 - user1506104
4
在下面的代码中,Play.class和Constants.ACTION_PLAY是什么意思?shortcutIntent = new Intent(getApplicationContext(), Play.class); shortcutIntent.setAction(Constants.ACTION_PLAY); 它们分别代表一个类和一个字符串常量。在这个例子中,Play.class可能是一个活动或服务的类名,而Constants.ACTION_PLAY可能是一个表示“播放”操作的字符串常量。shortcutIntent是一个用于启动另一个组件(如活动或服务)的意图对象,其中包括了要启动的组件的信息以及要执行的操作(即“播放”)。 - Dasharath Singh Bajroliya
显示剩余3条评论

35

除非通过软件升级,否则您无法更改已签名和封装的APK文件中的清单文件或资源。


2
@Nanne:那是应用程序小部件或主屏幕功能,而不是应用程序图标。你仍然不能通过软件升级以外的方式更改清单或资源。 - CommonsWare
1
不,我的意思是相反的:它并不是(被宣传为)一个小部件。我将其添加为应用程序快捷方式。但是,正如你所说,仅仅因为这个非股票东西暗示它只是一个图标,并不意味着它就是 :) - Nanne
2
@NeTeInStEiN:它不适用于所有主屏幕(例如,那些不关注组件启用更改的主屏幕)。 - CommonsWare
2
不再是真的了。 Android 6+ 上的 Google 日历每天在启动器中都会更改。今天,图标是“2”,昨天是“1”。通常,图标上会有“31”。但现在不再是这样了,它会更改。有人知道这是如何可能的吗? - UeliDeSchwert
1
@Sniper:“这个说法还有效吗?”-- 是的。“因为我看到日历每天都会更换图标。我正在使用Google Pixel。”-- Google编写了Android操作系统、Google日历应用程序和Pixel启动器。Google可以创建自己的钩子,以允许Google做想做的事情。 - CommonsWare
显示剩余25条评论

17

从程序上看,您可能希望自己发布应用程序启动器:

注意:此方法从Android 8.0 - Oreo开始不再适用。

在您的AndroidManifest.xml文件中添加:

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

那么您需要创建您的应用程序启动器意图:

Intent myLauncherIntent = new Intent();
myLauncherIntent.setClassName("your.package.name", "YourLauncherActivityName");
myLauncherIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

使用您的应用程序启动器和自定义图标创建安装快捷方式意图:

Intent intent = new Intent();
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, myLauncherIntent);
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "Application Name");
intent.putExtra
       (
        Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
        Intent.ShortcutIconResource.fromContext
                                    (
                                         getApplicationContext(), 
                                         R.drawable.app_icon
                                    )
       );
intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");

最后启动广播意图:

getApplicationContext().sendBroadcast(intent);

1
这将替换快捷方式图标,而不是启动器图标。 - user1506104

9

@P-A的解决方案对我部分有效。以下是我的发现:

1)第一段代码片段不正确,请参见下面:

<activity
    ...
    <intent-filter>
        ==> <action android:name="android.intent.action.MAIN" /> <== This line shouldn't be deleted, otherwise will have compile error
        <category android:name="android.intent.category.LAUNCHER" /> //DELETE THIS LINE
    </intent-filter>
</activity>

2) 在启用新图标之前,应使用以下代码禁用所有图标,否则它将添加一个新图标,而不是替换它。

注:该段文字为技术文档,已经尽可能通俗易懂。
getPackageManager().setComponentEnabledSetting(
        getComponentName(), PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);

但是,如果你使用上述代码,则主屏幕上的快捷方式将被删除!而且它不会自动添加回来。您可能能够以编程方式将图标添加回去,但它可能不会停留在之前的位置。

3)请注意,图标不会立即更改,可能需要几秒钟时间。如果您在更改后立即单击它,可能会收到一个错误,显示“应用程序未安装”。

因此,在我看来,这个解决方案只适用于更改应用程序启动器中的图标,而不适用于快捷方式(即主屏幕上的图标)。


2
运行应用程序时出错。找不到默认活动。 - CopsOnRoad
你删除了启动器,它怎么找到默认的活动 @Deqing? - j2emanue

8

如前所述,您需要使用<activity-alias>来更改应用程序图标。 为了避免在启用适当的activity-alias后终止应用程序,您需要在应用程序被终止后执行此操作。要找出应用程序是否已被终止,您可以使用此方法

  1. 在AndroidManifest.xml中创建活动别名
<activity android:name=".ui.MainActivity"/>

<activity-alias
    android:name=".one"
    android:icon="@mipmap/ic_launcher_one"
    android:targetActivity=".ui.MainActivity"
    android:enabled="true">

    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>

</activity-alias>

<activity-alias
    android:name=".two"
    android:icon="@mipmap/ic_launcher_two"
    android:targetActivity=".ui.MainActivity"
    android:enabled="false">

    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>

</activity-alias>
  1. 创建一个服务,在应用程序被杀死后更改活动的 activity-alias。需要将新的活动别名的名称存储在某处(例如SharedPreferences)。
class ChangeAppIconService: Service() {
    private val aliases = arrayOf(".one", ".two")

    override fun onBind(intent: Intent?): IBinder? = null

    override fun onTaskRemoved(rootIntent: Intent?) {
        changeAppIcon()
        stopSelf()
    }

    fun changeAppIcon() {
        val sp = getSharedPreferences("appSettings", Context.MODE_PRIVATE)

        sp.getString("activeActivityAlias", ".one").let { aliasName ->
            if (!isAliasEnabled(aliasName)) {
                setAliasEnabled(aliasName)
            }
        }
    }

    private fun isAliasEnabled(aliasName: String): Boolean {
        return packageManager.getComponentEnabledSetting(
            ComponentName(
                this,
                "${BuildConfig.APPLICATION_ID}$aliasName"
            )
        ) == PackageManager.COMPONENT_ENABLED_STATE_ENABLED
    }

    private fun setAliasEnabled(aliasName: String) {
        aliases.forEach {
            val action = if (it == aliasName)
                PackageManager.COMPONENT_ENABLED_STATE_ENABLED
            else
                PackageManager.COMPONENT_ENABLED_STATE_DISABLED
                
            packageManager.setComponentEnabledSetting(
                ComponentName(
                    this,
                    "${BuildConfig.APPLICATION_ID}$aliasName"
                ),
                action,
                PackageManager.DONT_KILL_APP
            )
        }
    }
}
  1. 在AndroidManifest.xml中添加服务
<service 
    android:name=".ChangeAppIconService"
    android:stopWithTask="false"
    />
  1. MainActivity.onCreate中启动ChangeAppIconService
class MainActivity: Activity {

    ...

    override fun onCreate(savedInstanceState: Bundle?) {
       ...

       startService(Intent(this, ChangeAppIconService::class.java))

       ...
    }

    ...

}

1
好的解决方案,Dmitry,谢谢。 但还有一个问题 - 你应该将这个方法添加到服务中: override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { Log.d("ChangeAppIconService", "Service Started") return START_NOT_STICKY } 这将处理当用户在托盘中滑动应用程序进程时的情况。 - Taras Vovkovych
仅当用户从“最近任务列表”中删除应用程序时,才会调用onTaskRemoved()函数。 - Jemshit Iskenderov
小米MIUI不会调用onTaskRemoved() :( - Jemshit Iskenderov

8
假设您的意思是更改主屏幕上显示的图标,这可以通过创建一个小部件来轻松完成。以下是一篇文章,演示了如何为类似于iPhone的“新消息”类型应用程序实现这一点: http://www.cnet.com/8301-19736_1-10278814-251.html

URL已失效。请尝试在此提供完整的答案,而不依赖于外部URL,因为这些很容易失效。 - undefined

6

尝试这个解决方案

<activity android:name=".SplashActivity"
        android:label="@string/app_name"
        android:icon="@drawable/ic_launcher">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity-alias android:label="ShortCut"
        android:icon="@drawable/ic_short_cut"
        android:name=".SplashActivityAlias"
        android:enabled="false"
        android:targetActivity=".SplashActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity-alias>

当您想要更改应用程序图标时,请添加以下代码

PackageManager pm = getPackageManager();
                    pm.setComponentEnabledSetting(
                            new ComponentName(YourActivity.this,
                                    "your_package_name.SplashActivity"),
                            PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
                            PackageManager.DONT_KILL_APP);

                    pm.setComponentEnabledSetting(
                            new ComponentName(YourActivity.this,
                                    "your_package_name.SplashActivityAlias"),
                            PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
                            PackageManager.DONT_KILL_APP);

以上代码运行良好。在连接和调试实时设备时,一切正常。但是当我生成一个发布版的.apk并安装后,应用程序会在没有任何日志的情况下关闭。有人有什么想法如何防止应用程序被杀死吗? - Dev app

4

AndroidManifest.xml示例:

<application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <activity android:name="com.pritesh.resourceidentifierexample.MainActivity"
                  android:label="@string/app_name"
                  android:launchMode="singleTask">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <!--<category android:name="android.intent.category.LAUNCHER"/>-->
            </intent-filter>
        </activity>

        <activity-alias android:label="RED"
                        android:icon="@drawable/ic_android_red"
                        android:name="com.pritesh.resourceidentifierexample.MainActivity-Red"
                        android:enabled="true"
                        android:targetActivity="com.pritesh.resourceidentifierexample.MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity-alias>

        <activity-alias android:label="GREEN"
                        android:icon="@drawable/ic_android_green"
                        android:name="com.pritesh.resourceidentifierexample.MainActivity-Green"
                        android:enabled="false"
                        android:targetActivity="com.pritesh.resourceidentifierexample.MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity-alias>

        <activity-alias android:label="BLUE"
                        android:icon="@drawable/ic_android_blue"
                        android:name="com.pritesh.resourceidentifierexample.MainActivity-Blue"
                        android:enabled="false"
                        android:targetActivity="com.pritesh.resourceidentifierexample.MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity-alias>

    </application>

然后在MainActivity中按照以下给出的代码:

ImageView imageView = (ImageView)findViewById(R.id.imageView);
            int imageResourceId;
            String currentDateTimeString = DateFormat.getDateTimeInstance().format(new Date());
            int hours = new Time(System.currentTimeMillis()).getHours();
            Log.d("DATE", "onCreate: "  + hours);

            getPackageManager().setComponentEnabledSetting(
                    getComponentName(), PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);

            if(hours == 13)
            {
                imageResourceId = this.getResources().getIdentifier("ic_android_red", "drawable", this.getPackageName());
                getPackageManager().setComponentEnabledSetting(
                        new ComponentName("com.pritesh.resourceidentifierexample", "com.pritesh.resourceidentifierexample.MainActivity-Red"),
                        PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
            }else if(hours == 14)
            {
                imageResourceId = this.getResources().getIdentifier("ic_android_green", "drawable", this.getPackageName());
                getPackageManager().setComponentEnabledSetting(
                        new ComponentName("com.pritesh.resourceidentifierexample", "com.pritesh.resourceidentifierexample.MainActivity-Green"),
                        PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);

            }else
            {
                imageResourceId = this.getResources().getIdentifier("ic_android_blue", "drawable", this.getPackageName());
                getPackageManager().setComponentEnabledSetting(
                        new ComponentName("com.pritesh.resourceidentifierexample", "com.pritesh.resourceidentifierexample.MainActivity-Blue"),
                        PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);

            }

            imageView.setImageResource(imageResourceId);

1
我遇到了“com.pritesh.resourceidentifierexample.MainActivity-Red在com.pritesh.resourceidentifierexample中不存在”的异常。这里我只是使用了您的清单名称来演示我的问题。 - Tejas Pandya
@PriteshPatel 上述代码运行良好。在连接和调试实时设备时,一切正常。但是当我生成一个发布版的.apk文件并安装它后,应用程序会在没有任何日志的情况下关闭。你知道为什么吗? - Dev app

3

应用上述建议后,我遇到了一个问题,当默认图标更改为新图标时,应用程序会崩溃。因此,我进行了一些调整并实现了代码。

第一步)在文件AndroidManifest.xml中,创建具有android:enabled="true"和其他alias的默认活动的<activity>标记,并使用android:enabled="false"为它们创建<activity-alias>标记。您的<application>标记将不包含任何<activity>标记,但是请将这些标记附加到具有android:enabled="true"的<activity-alias>标记中。

       <activity
        android:name=".activities.SplashActivity"
        android:label="@string/app_name"
        android:screenOrientation="portrait"
        android:theme="@style/SplashTheme">

    </activity>
    <!-- <activity-alias used to change app icon dynamically>   : default icon, set enabled true    -->
    <activity-alias
        android:label="@string/app_name"
        android:icon="@mipmap/ic_launcher"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:name=".SplashActivityAlias1" <!--put any random name started with dot-->
        android:enabled="true"
        android:targetActivity=".activities.SplashActivity"> <!--target activity class path will be same for all alias-->
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity-alias>
    <!-- <activity-alias used to change app icon dynamically>  : sale icon, set enabled false initially -->
    <activity-alias
        android:label="@string/app_name"
        android:icon="@drawable/ic_store_marker"
        android:roundIcon="@drawable/ic_store_marker"
        android:name=".SplashActivityAlias" <!--put any random name started with dot-->
        android:enabled="false"
        android:targetActivity=".activities.SplashActivity"> <!--target activity class path will be same for all alias-->
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity-alias>

第二步:创建一个方法,用于禁用包含默认图标的第一个activity-alias并启用包含需要更改的图标的第二个alias。
/**
 * method to change the app icon dynamically
 *
 * @param context
 * @param isNewIcon  : true if new icon need to be set; false to set default 
 * icon
 */

public static void changeAppIconDynamically(Context context, boolean isNewIcon) {
    PackageManager pm = context.getApplicationContext().getPackageManager();
    if (isNewIcon) {
        pm.setComponentEnabledSetting(
                new ComponentName(context,
                        "com.example.dummy.SplashActivityAlias1"), //com.example.dummy will be your package
                PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
                PackageManager.DONT_KILL_APP);

        pm.setComponentEnabledSetting(
                new ComponentName(context,
                        "com.example.dummy.SplashActivityAlias"),
                PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
                PackageManager.DONT_KILL_APP);
    } else {
        pm.setComponentEnabledSetting(
                new ComponentName(context,
                        "com.example.dummy.SplashActivityAlias1"),
                PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
                PackageManager.DONT_KILL_APP);

        pm.setComponentEnabledSetting(
                new ComponentName(context,
                        "com.example.dummy.SplashActivityAlias"),
                PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
                PackageManager.DONT_KILL_APP);
    }
}

步骤三)。根据您的需求调用此方法,例如在按钮单击、日期特定或场合特定条件下,只需像这样简单地调用:

// Switch app icon to new icon
    GeneralUtils.changeAppIconDynamically(EditProfileActivity.this, true);
// Switch app icon to default icon
            GeneralUtils.changeAppIconDynamically(EditProfileActivity.this, false);

希望这能帮助那些遇到应用程序在更改图标时被终止的人。 编码愉快 :)

以上代码运行良好。在连接和调试实时设备时没有问题,但是当我生成一个发布版.apk并安装它时,应用程序会在没有任何日志的情况下关闭。你知道为什么吗? - Dev app
@Abhijeet,有当前答案的任何线索吗? - gumuruh

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接