美文网首页
Android如何动态切换APP图标?

Android如何动态切换APP图标?

作者: 夏木友人 | 来源:发表于2022-11-02 11:06 被阅读0次

一、在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

相关文章