APP多个图标以及名称动态更换
首先是权限
<uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES" />
其次是要启动的launch app
<activity
android:name=".main"
android:icon="图标"
android:label="名称"
android:configChanges="keyboardHidden|orientation|screenSize|locale|layoutDirection|screenLayout|mcc|mnc|touchscreen|keyboard|keyboardHidden|navigation"
android:launchMode="singleTop"
android:windowSoftInputMode="adjustResize|stateHidden" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
其次是它的其他进入路口,也就是使用activity-alias
图标,名称都可以自定义,只需要注意targetActivity要同launch。
name是后面启动的入口类,不要相同。
一开始建议enable都为false,不然主页会有两个图标显示。
<activity-alias
android:name=".sha1"
android:enabled="false"
android:icon="图标1"
android:label="名称1"
android:targetActivity=".main">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity-alias>
<activity-alias
android:name=".sha2"
android:enabled="false"
android:icon="图标2"
android:label="名称2"
android:targetActivity=".main">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity-alias>
<activity-alias
android:name=".sha3"
android:enabled="false"
android:icon="图标3"
android:label="名称3"
android:targetActivity=".main">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity-alias>
需要注意的是替换图标及名称建议在launch类里面进行,不然会出现莫名无法启动的问题,可能跟启动方式有关,所以后面启动方式我都改为了用包名启动,这样不会有问题。
动态更改图标
写详细一点,可以自己考虑精简
注意DISABLED和ENABLED的设定。
PackageManager pm = getPackageManager();
以下其中xxx为应用包名
替换为sha1
pm.setComponentEnabledSetting(getComponentName(),PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP); //关掉默认启动
pm.setComponentEnabledSetting( new ComponentName(getBaseContext(),"xxx.xxx.xxx.sha2"),PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);//关掉sha2
pm.setComponentEnabledSetting( new ComponentName(getBaseContext(),"xxx.xxx.xxx.sha3"),PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);//关掉sha3
pm.setComponentEnabledSetting(new ComponentName(getBaseContext(), "xxx.xxx.xxx.sha1"), PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);//启动sha1
剩下的以此类推
替换为sha2
pm.setComponentEnabledSetting(getComponentName(),PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
pm.setComponentEnabledSetting( new ComponentName(getBaseContext(),"xxx.xxx.xxx.sha1"),PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
pm.setComponentEnabledSetting( new ComponentName(getBaseContext(),"xxx.xxx.xxx.sha3"),PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
pm.setComponentEnabledSetting(new ComponentName(getBaseContext(), "xxx.xxx.xxx.sha2"), PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
替换为sha3
pm.setComponentEnabledSetting(getComponentName(),PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
pm.setComponentEnabledSetting( new ComponentName(getBaseContext(),"xxx.xxx.xxx.sha1"),PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
pm.setComponentEnabledSetting( new ComponentName(getBaseContext(),"xxx.xxx.xxx.sha2"),PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
pm.setComponentEnabledSetting(new ComponentName(getBaseContext(), "xxx.xxx.xxx.sha3"), PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
在这里要注意的是,如果要启动此入口类app,在首次启动时用
Intent intent = new Intent();
intent.setClassName("xxx.xxx.xxx","xxx.xxx.xxx.main");
startActivity(intent);
替换后启动方式为
Intent intent = new Intent();
intent.setClassName("xxx.xxx.xxx","xxx.xxx.xxx.sha1");
startActivity(intent);
只需要保存更改后的包名即可,类似文章来源:https://www.toymoban.com/news/detail-756709.html
String play = getSharedPreferences("syswj", Context.MODE_PRIVATE).getString("play","xxx.xxx.xxx.main");
Intent intent = new Intent();
intent.setClassName("xxx.xxx.xxx",play);
startActivity(intent);
时隔多年,又写文咯o( ̄︶ ̄)o文章来源地址https://www.toymoban.com/news/detail-756709.html
到了这里,关于安卓动态更换图标,这一篇应该够详细了吧~的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!