1.第一步 通过activity-alias别名实现,manifest 这里写的是一个默认的图标Default和一个需要切换的图标Test,以及一个默认的首页面HomeActivity:
<!-- 默认的图标--> <activity-alias android:name=".activity.Default" android:icon="@mipmap/logo" android:label="@string/app_name" android:targetActivity=".activity.HomeActivity" android:enabled="false" android:exported="true" android:configChanges="orientation|navigation|keyboardHidden" android:theme="@style/AppThemeNoAnimation" android:launchMode="singleTop" android:excludeFromRecents="true" android:screenOrientation="portrait"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity-alias> <!-- 需要切换的图标--> <activity-alias android:name=".activity.Test" android:label="短信" android:icon="@mipmap/ic_launcher" android:targetActivity=".activity.HomeActivity" android:enabled="false" android:exported="true" android:configChanges="orientation|navigation|keyboardHidden" android:theme="@style/AppThemeNoAnimation" android:launchMode="singleTop" android:excludeFromRecents="true" android:screenOrientation="portrait"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity-alias> <!-- 默认activity--> <activity android:name=".activity.HomeActivity" android:launchMode="singleTop" android:screenOrientation="portrait" android:windowSoftInputMode="adjustPan"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>
2.第二步 实现切换代码:
/** * 修改图标和名称的方法 * * @param enable */ public void changeIcon(boolean enable) { PackageManager pm = getApplicationContext().getPackageManager(); if (enable) { //显示Test图标 pm.setComponentEnabledSetting(new ComponentName( getBaseContext(), "com.qiyou.wx.activity.Test"), PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP); //去掉Default图标 pm.setComponentEnabledSetting(new ComponentName( getBaseContext(), "com.qiyou.wx.activity.Default"), PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP); //去掉HomeActivity图标 pm.setComponentEnabledSetting(new ComponentName( getBaseContext(), "com.qiyou.wx.activity.HomeActivity"), PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP); Log.d("TAG", "换Test的图标"); } else { // //显示Default图标 pm.setComponentEnabledSetting(new ComponentName( getBaseContext(), "com.qiyou.wx.activity.Default"), PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP); //去掉Test图标 pm.setComponentEnabledSetting(new ComponentName( getBaseContext(), "com.qiyou.wx.activity.Test"), PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP); //去掉HomeActivity图标 pm.setComponentEnabledSetting(new ComponentName( getBaseContext(), "com.qiyou.wx.activity.HomeActivity"), PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP); Log.d("TAG", "换Default的图标"); } }
3.第三步 代码调用:
//使用Test图标 changeIcon(true); //使用Default图标 changeIcon(false);
使用Android studio运行时,运行几次就运行不了了,提示找不到活动类:
出现此错误,解决办法如下: