1.修改manifest,增添第二入口类TestAlias
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test.liwei">
<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:exported="true"
android:label="哈哈哈"
android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".TestAlias"
android:exported="true" >
</activity>
<activity-alias
android:enabled="false"
android:icon="@drawable/timg"
android:label="@string/app_name"
android:targetActivity=".TestAlias"
android:name=".TestAlias">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity-alias>
</application>
</manifest>
主要是下面
<activity
android:name=".TestAlias"
android:exported="true" >
</activity>
<activity-alias
android:enabled="false"
android:icon="@drawable/timg"
android:label="@string/app_name"
android:targetActivity=".TestAlias"
android:name=".TestAlias">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity-alias>
2.在ACTIVITY调用如下方法
private void changeIcon() {
PackageManager pm = getApplicationContext().getPackageManager();
System.out.println(getComponentName());
//去除旧图标,不去除的话会出现2个App图标
pm.setComponentEnabledSetting(getComponentName(),
PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP);
//显示新图标
pm.setComponentEnabledSetting(new ComponentName(
getBaseContext(),
"com.test.liwei.TestAlias"),//此处包名需要修改
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP);
}
网友评论