Android如何动态切换APP图标?

一、在AndroidManifest 配置三个图标

<activity-alias
            android:icon="@mipmap/ic_launcher"
            android:name=".MainActivity"
            android:targetActivity=".MainActivity"
            android:enabled="true"
            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:icon="@mipmap/icon2"
            android:name=".icon2"
            android:targetActivity=".MainActivity"
            android:enabled="false"
            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:icon="@mipmap/icon3"
            android:name=".icon3"
            android:enabled="false"
            android:targetActivity=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity-alias>

二、切换图标方法

   /**
     * 更新别名显示
     * @param componentName componentName
     * @param enable 是否启用
     */
    private void updateAlias(Boolean enable, ComponentName componentName) {
        int newState;
        if (enable){
            newState = PackageManager.COMPONENT_ENABLED_STATE_ENABLED;
        }else {
            newState = PackageManager.COMPONENT_ENABLED_STATE_DISABLED;
        }
        getPackageManager().setComponentEnabledSetting(componentName, newState, PackageManager.DONT_KILL_APP);
    }

三、创建一个新页面,里面增加三个按钮,分别替换三个不同的APP图标

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private ComponentName defaultComponent;
    private ComponentName icon2Component;
    private ComponentName icon3Component;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        defaultComponent = new ComponentName(this, getPackageName()+".MainActivity");
        icon2Component = new ComponentName(this, getPackageName()+".icon2");
        icon3Component = new ComponentName(this, getPackageName()+".icon3");

        findViewById(R.id.btn1).setOnClickListener(this);
        findViewById(R.id.btn2).setOnClickListener(this);
        findViewById(R.id.btn3).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.btn1){
            updateAlias(true, defaultComponent);
            updateAlias(false, icon2Component);
            updateAlias(false, icon3Component);
        }else if (v.getId() == R.id.btn2){
            updateAlias(false, defaultComponent);
            updateAlias(true, icon2Component);
            updateAlias(false, icon3Component);
        }else if (v.getId() == R.id.btn3){
            updateAlias(false, defaultComponent);
            updateAlias(false, icon2Component);
            updateAlias(true, icon3Component);
        }
    }

四、当然这只是demo演示,至于自己想什么时候替换APP图标,根据自己的具体业务。

  1. 一般可以根据收到服务端切换图标的消息,然后去动态更换图标。
  2. 由于更换图标会导致先退出APP,所以我们可以在APP退到后台时,再去更换APP图标,不然会导致用户误以为是闪退.

demo地址
https://github.com/liyuyitong/LaucherIconModify

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 前言 Google Play应用市场对于应用的targetSdkVersion有了更为严格的要求。从 2018 年...
    申国骏阅读 64,815评论 15 98
  • 《来,我们说说孤独》 1·他们都在写孤独 一个诗人 如果 不说说 内心的孤独 不将孤独 写进诗里 是不是很掉价呢 ...
    听太阳升起阅读 4,445评论 1 7
  • 自幼贫民窟长大的女子,侥幸多念了两本书,枉以为可以与人平起平坐。可是人生从来都是接力赛,我们却天真的当成了百米冲刺...
    Leeanran阅读 5,811评论 1 5
  • 云舒老师,姓甚名谁,男的女的,多大岁数,这些我全然不知。之所以要写写云舒老师,完全是因为他写的文章,如一个巨大的磁...
    数豆者m阅读 2,442评论 6 9
  • """1.个性化消息: 将用户的姓名存到一个变量中,并向该用户显示一条消息。显示的消息应非常简单,如“Hello ...
    她即我命阅读 3,324评论 0 5