Recently I saw an article written by “doctor” : I changed a line of code last time! “, is really rich imagination, did not expect android app desktop icon can also dynamically change, increase knowledge, contact Android development is the first time to know this thing, feel ashamed. Then carefully read, start to practice, to which the knowledge points involved in the official documents to understand some, summary record here.

Case Studies & Sources of demand


During the Double 11 shopping Festival, you will find that the desktop icon of Handtao App will change, of course, the theme color in the App will also change, becoming very festive, creating a festive atmosphere, user experience is excellent. Since there is such a way of product operation, how to achieve it technically? Modification of application theme tone is not discussed here, common effects are black and white theme switch, theme package download, etc., on the corresponding development and implementation of the explanation, a lot of relevant information on the Internet. Here’s how to modify the desktop icon, after all, the knowledge involved in this point is still rare.

In fact, it is very simple, using the

tag in the androidmanifest.xml file.

<activity-alias>introduce


As you know, when using the Activity component, you need to register the name, theme, intent-filter and other related attributes in the Manifest file through the label, and then start the corresponding Activity through the intent action. You can also use the

tag to register an “alias” for each activity, which can also start the corresponding target activity. Let’s take a look at what properties this alias can set:

<activity-alias android:enabled=["true" | "false"]
                    android:exported=["true" | "false"]
                    android:icon="drawable resource"
                    android:label="string resource"
                    android:name="string"
                    android:permission="string"
                    android:targetActivity="string" >
        . . .
        </activity-alias>
        Copy the code











  • Android: Enabled attribute, Boolean type, whether to enable alias setting. Default value: true.

  • Android: Exported attribute, Boolean, whether to support other applications using this alias to access the target Activity. The default value is true.

  • Android: Icon and Label attributes: similar to the

    tag, represents the target activity display icon and label;

  • Android :name attribute: alias of the Activity. In the < Activity > tag, the name attribute must be consistent with the name of the corresponding Activity file, and the alias can be arbitrarily set to ensure uniqueness.

  • Android: Permission property :permission Settings, which restrict the use of aliases. For details, refer to the description of permissions on the developer’s official website.

  • Android: targetActivity attribute: Specify the target Activity that the alias can start. Note that the attribute value must correspond to the name attribute in the < Activity > tag, and that the < Activity > tag must precede the

    tag.

We practice


Now that you know the basics of

, you know how to dynamically change desktop ICONS and application names. Add an

tag to the Activity entry of the entire application, set the desktop icon and application name to be the same as the

attribute, and start the Activity dynamically.


For example, the Manafest file key code is as follows:

<application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="Samples"
        android:supportsRtl="true"
        android:name=".MyApplication"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity-alias
            android:name=".MainAliasActivity"
            android:targetActivity=".MainActivity"
            android:label="Samples Alias"
            android:icon="@mipmap/ic_launcher_alias"
            android:enabled="false">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity-alias>
        </application>
        Copy the code

Note that the

tag was added to the alias Settings, consistent with the targetActivity setting:

  • android.intent.action.MAINIndicates that the alias is the entry point to the entire application. The Activity is the first created when the application starts.
  • android.intent.category.LAUNCHERIndicates that this alias setting will appear on the desktop Launcher application;

For other properties, the

tag has Settings as well, but they are generally set in the < Application > tag, and the

tag inherits the Settings in the < Application > tag by default. One other thing to note about the code above is that the Android: Enabled property is set to false, otherwise the runtime will have two app ICONS and names on the desktop with the same functionality but different displays.

Then dynamically switch in the Activity, through the PackageManager object provide setComponentEnabledSetting () method to close the current Component components, and start the corresponding Component alias Component, The reference code is as follows:

public void onClickOne(View v){
        PackageManager pm = getPackageManager();
        pm.setComponentEnabledSetting(getComponentName(),
                PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
        pm.setComponentEnabledSetting(new ComponentName(this, "com.yifeng.samples.AliasName"),
                PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
                }
                Copy the code

The effect is as follows:

Note that after you make the changes, you need to wait a little while to see the changes. If you want to see the change immediately after the change is complete, restart the application Launcher with an Intent. The code is as follows:

Intent intent = new Intent(Intent.ACTION_MAIN);
    intent.addCategory(Intent.CATEGORY_HOME);
    intent.addCategory(Intent.CATEGORY_DEFAULT);
    List<ResolveInfo> resolves = pm.queryIntentActivities(intent, 0);
    for (ResolveInfo res : resolves) {
        if (res.activityInfo != null) {
            ActivityManager am = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
            am.killBackgroundProcesses(res.activityInfo.packageName);
        }
        }
        Copy the code

Then don’t forget to add permissions to the Manifest file:

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

With these Settings, you can basically change your app’s desktop icon and name on the fly. Usually, a new icon can be placed in the application in advance, and the icon and name displayed on the App Launcher can be flexibly triggered by the behavior of message push on the server in a certain period of time. In this way, there is no need to iterate a new version to change an icon. It is worth noting that from a product perspective, generally not used for short-term an activity to modify the name, but only change the desktop icon, and a new desktop icon is on the basis of the original style move hands and feet, have the effect of icing on the cake, don’t have to face all, otherwise you will let users misunderstandings, that thed loss outweights the gain.

legacy


First, the above Settings can only modify the application icon and name of the Launcher, which belong to the application level and cannot be modified at the OS level. For example, after modifying the application, use the Menu physical key to open the Multi-Task window. Or go to Settings and look at the list of apps, and you’ll see that the icon and name of the corresponding app are still displayed as the default. However, for ordinary users, it is mainly about the display on the desktop Launcher, which is the most intuitive and commonly used place after all.

Second, here we pre-place the new desktop Icon in the application resource file and specify the corresponding reference in the

tag. Is there a way to set the Icon property in Java code? If so, it would be perfect. Wouldn’t it be more flexible to use the icon file on the server? After some efforts, I still failed to find a corresponding solution. If you know, please leave a message or follow my wechat public account (search: NiaoTech) to communicate with me. Thank you.

To develop


You can also add desktop shortcuts to your App using the

tag. The steps are the same. But there’s another implementation available for you: App Shortcuts.

This article is created by Also Feng and first published in also Feng’s personal blog, simultaneously authorized wechat public number: Technical bird (NiaoTech).

Welcome all forms of exchange and reprint, indicate the author and source.

Android uses

to dynamically change App desktop ICONS

This article links to: yifeng. Studio / 2016/12/30 /…