个人博客:
http://www.milovetingting.cn

动态更新Icon

动态更改图标主要用到activity-aliasPackageManager的setComponentEnabledSetting方法。具体步骤如下:

1、在AndroidManifest.xml中增加以下声明:

1
2
3
4
5
6
7
8
9
10
11
12
<activity-alias
android:name=".alias"
android:enabled="false"
android:icon="@mipmap/icon"
android:label="Alias"
android:targetActivity=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

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

完整文件如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
   <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.wangyz.dynamicicon">

<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=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

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

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

</manifest>

2、切换图标

1
2
3
4
5
6
7
getPackageManager().setComponentEnabledSetting(getComponentName(),
PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP);
getPackageManager().setComponentEnabledSetting(new ComponentName(this,
getPackageName() + ".alias"),
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
0);

setComponentEnabledSetting方法的最后一个参数,PackageManager.DONT_KILL_APP表示不关闭App,0表示关闭App刷新图标。

完整文件如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
   package com.wangyz.dynamicicon;

import android.content.ComponentName;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

public void changeIcon(View view) {
getPackageManager().setComponentEnabledSetting(getComponentName(),
PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP);
getPackageManager().setComponentEnabledSetting(new ComponentName(this,
getPackageName() + ".MainActivity"),
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
0);
}

public void changeIconAlias(View view) {
getPackageManager().setComponentEnabledSetting(getComponentName(),
PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP);
getPackageManager().setComponentEnabledSetting(new ComponentName(this,
getPackageName() + ".alias"),
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
0);
}
}

源码地址:https://github.com/milovetingting/Samples/tree/master/DynamicIcon