SoFunction
Updated on 2025-03-11

Android implements example code to dynamically change app icons

This article introduces the dynamic change app icon and shares it with everyone, as follows:

The code implementation is as follows:

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

 <activity android:name=".MainActivity">
 <intent-filter>
 <action android:name="" />
 <category android:name="" />
 </intent-filter>
 </activity>

 <activity-alias
 android:name=".newsLuncherActivity"
 android:enabled="false"
 android:icon="@mipmap/app_logo"
 android:label="@string/app_name"
 android:targetActivity=".MainActivity">
 <intent-filter>
 <action android:name="" />
 <category android:name="" />
 </intent-filter>
 </activity-alias>

</application>

Let's first briefly analyze the above code:

android:icon="@mipmap/app_logo"
android:label="@string/app_name"

These two properties are used to set icons and labels

android:name=".newsLuncherActivity"

Set the name of the alias to an activity, try to keep it consistent with the name of <activity>. If it is not set to an activity name, some mobile phones will have problems.

android:enabled="false"

Whether it is a display alias, the default is true.

android:targetActivity=".MainActivity"

Specify the activity started by the alias. It must be consistent with the name of the original startup entrance activity and be under the tag of <activity>.

After adding the <activity-alias> tag, we can set a switch on the server side. When we request to change the desktop icon, we can close the current Component component through the setComponentEnabledSetting() method provided by the PackageManager object and start the Component component corresponding to the alias. In order to enable the icon to be changed quickly, we can add the restart of the Luncher application code. The name is to define a class name yourself. Remember to pass the full path, such as:



private void changeLuncher(String name) {
    PackageManager pm = getPackageManager();
    (getComponentName(),
        PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
    (new ComponentName(, name),
        PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
    //Intent Restart the Launcher application    Intent intent = new Intent(Intent.ACTION_MAIN);
    (Intent.CATEGORY_HOME);
    (Intent.CATEGORY_DEFAULT);
    List&lt;ResolveInfo&gt; resolves = (intent, 0);
    for (ResolveInfo res : resolves) {
      if ( != null) {
        ActivityManager am = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
        ();
      }
    }
  }

The alias <activity-alias> can be defined multiple times, and different icons can be dynamically replaced at different times.

Permission settings:

Copy the codeThe code is as follows:

<uses-permission android:name=".KILL_BACKGROUND_PROCESSES" />

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.