android android studio java 动态切换APP的Logo和Name - TestAlias"
动态切换APP的Logo和Name
本篇介绍如何动态切换APK的图标和名字
前言: 工作中,有时候会遇到需要在某个时候切换APP的图标和名字,最好是要能通过后台控制。
重心: APP虽然索不到动态去加载后台的图片来切换本身Logo,但是可以间接去实现。这里就是本篇所要介绍的,图标、名字都是提前配置好打进去APK的。
切换图标和名字重点代码:
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.icondemo.TestAlias2"),
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP);
清单文件中要配置对应的Activity-alias,顾名思义别名的意思。即一个APP应用可以配置多个别名和多个入口,通过上述代码去切换APP的入口,重新加载指定的Alias,从而达到切换LOGO和改变Name的效果:
package="com.test.icondemo">
android:name=".MyApp"
android:theme="@style/ContentOverlay"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="Samples"
android:supportsRtl="true"
>
android:exported="true">
android:enabled="true"
android:label="应用名称"
android:name=".MainActivity"
android:targetActivity=".MainActivity"
android:exported="true">
android:name=".TestAlias"
android:label="入口2"
android:exported="true" > 拼多多帮助力入口
android:enabled="false"
android:icon="@mipmap/ic_new"
android:label="测试2"
android:name=".TestAlias"
android:targetActivity=".TestAlias"
android:exported="true">
android:name=".TestAlias2"
android:label="入口2"
android:exported="true" >
android:enabled="false"
android:icon="@mipmap/ic_new2"
android:label="测试3"
android:name=".TestAlias2"
android:targetActivity=".TestAlias2"
android:exported="true">
这是本博主的完整清单文件和调用方式。这里需要注意的是,当你切换了APP的入口,APP真实也会进入对应的Activity-alias页面。,好了,本篇完。
相关链接