Introduction to the

Recently met such a demand, dynamic change desktop ICONS, the function was more common in itself, such as Tmall app in various basic chop hand day will change the appropriate icon and integral style, but only discuss the change icon here, the access to some data, introduce here I think the most convenient way.

The principle of

It’s very simple to use the <activity-alias> tag to control whether the enabled attribute is displayed or not.

implementation

Before implementation, briefly describe the requirements:

The default icon of the App is the Android robot icon, and there are two other ICONS, icon_1 and Icon_2. Click the button to set which icon becomes, and there is also a function to restore the icon.

Example – effect.apk

Implementation steps:

  • Add a corresponding number of

    tags
  • Add layout drinks corresponding to click events
  • The code controls which icon is displayed

Let’s do this step by step:

1. Add a corresponding number of <activity-alias> tags

There are two more ICONS, so we add two more <activity-alias> tags. This tag is in the <application> tag of androidmanifest.xml, the same level as the <activity> tag, and one of them looks like this:

<activity-alias android:name=".MainActivity1" android:enabled="false" android:icon="@mipmap/icon_1" android:label="@string/app_name" android:targetActivity=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN"/> <category android:name="android.intent.category.LAUNCHER"/> </intent-filter>  </activity-alias>Copy the code

Here are a few attributes to note:

attribute meaning
name The value can be any, as long as it is unique. It is recommended to be regular for convenient management
targetActivity The value of this attribute represents the Activity being pointed to, and the tag itself represents the alias of the Activity. Remember to declare the Activity pointing to before the tag, otherwise it may not run
icon Indicates the application icon corresponding to the alias
label Indicates the application name corresponding to the alias
enabled <activity-alias> <activity-alias> <activity-alias> <activity-alias> <activity-alias> <activity-alias> <activity-alias> <activity-alias

< activity-filter > < activity-filter > < activity-filter > < activity-filter > < activity-filter > < activity-filter > < activity-filter > < activity-filter > < activity-filter >

The other one is the same, but I won’t go into it here.

2. Add layout and click events

The layout is a vertical LinearLayout with three buttons. Use the onClick attribute to set the corresponding click method. Of course, you can also get these buttons and set the OnClickListener. The Demo address is at the end of the article.

3. The code controls which icon to display

This step is actually to call a method in PackageManager, the method is as follows:

private void changeLauncher(String name) { PackageManager pm = getPackageManager(); / / hide before the show desktop components of PM. SetComponentEnabledSetting (getComponentName (), PackageManager.COM PONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP); / / show the new desktop components PM. SetComponentEnabledSetting (new the ComponentName (MainActivity. This name), PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP); }Copy the code

Are called PackageManager setComponentEnabledSetting method, which is the first component parameter operation, the second parameter according to show or hide, the third component represents whether to turn off the app.

TIPS

1, there is a skill, it is suggested that do not directly point the switch button to execute the icon, because after a switch icon will always close the app to open, so we can click record which icon to switch to first, when the program exits switch icon, so that would not close the app.

2, careful friends will find, in the debugging stage, I changed the app start icon, and then execute the code to start, found that won’t start, actually this is because the code enabled by default in the components and modified the components not consistent, so can’t start, but there is no influence for program updates and install.

3, this way, the dynamic change icon, not replace, switch to take effect immediately after, should be related to mobile phone performance according to my observation, after do this, will in a few seconds to change the icon, but for the change of the ordinary desktop ICONS, the faults was acceptable, after all, not the user manual trigger, nor experience.

DEMO

Example – Portal

Thank you

Android dynamic change desktop icon