当前位置:网站首页>21-day Learning Challenge--Pick-in on the third day (dynamically change the app icon)
21-day Learning Challenge--Pick-in on the third day (dynamically change the app icon)
2022-08-06 08:53:00 【Meng Fangfang】
1.图标设置
appIcons are generally set inAndroid manifest.xml文件里application的icon属性里,如下:
Android manifest.xml:
<application
android:icon="mipmap/ic_launcher"
……>
淘宝、Weibo has a default icon,But when it comes to major festivals or important events,Their icons will change accordingly,这是怎么做到的呢?
2.Dynamically change icons
实现的关键是使用activity-alias,Configure multiple identicalactivity.
实现流程为:在Manifest文件中使用activity-aliasPrepare multiple labelsActivity入口,每个activity都指向入口Activity,And for each ownedactivity-alias标签的activity设置单独的icon和应用名,最后调用SystemService 服务kill掉launcher,并执行launcher的重启操作.
①首先,修改AndroidManifest.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.demo.test">
<!-- 权限-->
<uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES"/>
<application
android:allowBackup="true"
android:icon="@mipmap/default_logo"
android:label="@string/app_name"
android:roundIcon="@mipmap/default_logo"
……>
...
<!-- 默认图标-->
<activity-alias
android:name="com.demo.test.default"
android:targetActivity=".MainActivity"
android:label="@string/app_name"
android:enabled="false"
android:icon="@mipmap/default_logo"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity-alias>
<!-- 活动图标-->
<activity-alias
android:name="com.demo.test.special"
android:targetActivity=".MainActivity"
android:label="@string/special"
android:enabled="false"
android:icon="@mipmap/special_logo"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity-alias>
...
</application>
</manifest>
properties involved:
android:name:注册的组件名字,启动组件的名称.
android:enabled:是否启用这个组件,That is, whether to display this entry.
android:icon:图标
android:label:名称
android:targetActivity:默认的activity没有这个属性,指定目标activity,与默认的activity中的name属性是一样的,需要有相应的java类文件.
注:
(1)activity-alias需要在activity下面,enabled为false,Otherwise there will be two icons on the desktop
(2)activity-alias下的name随意,但targetActivityneed to be aboveactivity的name
(3)The configuration needs to be changed hereicon和label
(4)android:exported都需要为true.默认为true
②在MainActivity触发Logo图标更换逻辑:
private void changeIcon(String name){
PackageManager pm = getPackageManager();
pm.setComponentEnabledSetting( getComponentName(),PackageManager.COMPO NENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
pm.setComponentEnabledSetting(new ComponentName(this, name), PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
restart(pm);
}
注意changeIcon()方法中的nameStrings require andAndroidManifest.xml文件中的<activity-alias>的name相对应.
边栏推荐
- [图]Edge 104稳定版发布:引入“增强安全模式”
- 第十六天(配置BPDU,TCN BPDU)
- Tencent Cloud VOD uploads video files to solve the path problem
- ROS报错[rospack] Error: package ‘.....‘ not found
- HCIP第十八天笔记
- The 22nd day of the special assault version of the sword offer
- 20220803 Simulation
- Rock vocal | completes the safety operation, is not so difficult as you think
- 干货,分布式数据库在金融核心场景的落地实践|腾讯云数据库
- ACM common header files
猜你喜欢
随机推荐
dalle2:hierarchical text-conditional image generation with clip
how to jump higher
UNIX environment advanced programming - the first chapter
SPFA模板
Free and open source web version of Xshell [Happy New Year to everyone]
华为外包测试2年,不甘被替换,168天的学习转岗成正式员工
errorCode 1045, state 28000错误详解即解决方法
从“草原牛”到“数字牛”:蒙牛的数字化转型之道!
Hdu 2022多校训练(5) Slipper
BigEvent Demo
项目运维工作的心得总结
数组里的值放到另一个数组中,并转大写
Folyd
bpe Chinese tokens
VLAN experiment
免费的开源的 网页版Xshell【同祝贺大家新年快乐】
Xshell下载 破解,史上最简单易懂教程
腾讯云点播上传视频文件解决路径问题
Flashing Neon Text Animation
Day 17 (16 day bpdus related knowledge and STP configuration)









