114.Android简单的动态切换app的图标,两种方式
第一种方式:
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运行时,运行几次就运行不了了,提示找不到活动类:
出现此错误,解决办法如下:
选择nothing即可
第二种方式:
1.第一步 通过activity实现,manifest 这里写的是一个默认的HomeActivity和一个需要切换的TestActivity:
<activity android:icon="@mipmap/logo" android:label="@string/app_name" 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> <activity android:icon="@mipmap/ic_launcher" android:label="Text name" android:name=".activity.TextActivity" 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.第二步 创建TextActivity并继承默认的HomeActivity:
public class TextActivity extends HomeActivity { }
3.第三步 切换代码:
/** * 修改图标和名称的方法 * * @param enable */ public void changeIcon(boolean enable) { PackageManager pm = getApplicationContext().getPackageManager(); if (enable) { //显示TextActivity图标 pm.setComponentEnabledSetting(new ComponentName( getBaseContext(), "com.qiyou.wx.activity.TextActivity"), PackageManager.COMPONENT_ENABLED_STATE_ENABLED, 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", "换TextActivity的图标"); } else { //显示HomeActivity图标 pm.setComponentEnabledSetting(new ComponentName( getBaseContext(), "com.qiyou.wx.activity.HomeActivity"), PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP); //去掉TextActivity图标 pm.setComponentEnabledSetting(new ComponentName( getBaseContext(), "com.qiyou.wx.activity.TextActivity"), PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP); Log.d("TAG", "换HomeActivity的图标"); } }
4.第四步 代码调用:
//使用TextActivity图标 changeIcon(true); //使用HomeActivity图标 changeIcon(false);
//如果想要app有多个图标,只需要新建几个activity页面并添加android:documentLaunchMode="intoExisting" 属性即可。
//-------------------------------------------------------------END------------------------------------------------------------