更换APP启动图标和名称

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/ffengz/article/details/76546613

情景

类似于双11等节日,实现app图标的动态更换

效果展示

手机比较慢,见谅-_-!!!

Launcher Icon

实现

1.在manifest文件为MainActivity创建别名

这里将别名设置为不可用,到时候再使用。

        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

        <!--红色图标-->
        <activity-alias
            android:name="icon1"
            android:enabled="false"
            android:icon="@mipmap/icon_red"
            android:label="@string/app_name_1"
            android:targetActivity=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

        <!--蓝色图标-->
        <activity-alias
            android:name="icon2"
            android:enabled="false"
            android:icon="@mipmap/icon_blue"
            android:label="@string/app_name_2"
            android:targetActivity=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

2.获取ComponentName和PackageManager

    private ComponentName mDefault;
    private ComponentName mRed;
    private ComponentName mBlue;
    private PackageManager mPackageManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mDefault = new ComponentName(getBaseContext(), "com.hftsoft.changelaunchericon.MainActivity");
        mRed = new ComponentName(getBaseContext(), "com.hftsoft.changelaunchericon.icon1");
        mBlue = new ComponentName(getBaseContext(), "com.hftsoft.changelaunchericon.icon2");
        mPackageManager = getApplicationContext().getPackageManager();
    }

3.动态的配置启动图标

    /**
     * 启用组件
     *
     * @author fengzhen
     * @version v1.0, 2017/8/1 14:46
     */
    private void enableComponent(ComponentName componentName) {
        mPackageManager.setComponentEnabledSetting(
                componentName,
                PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
                PackageManager.DONT_KILL_APP);
    }

    /**
     * 停用组件
     *
     * @author fengzhen
     * @version v1.0, 2017/8/1 14:46
     */
    private void disableComponent(ComponentName componentName) {
        mPackageManager.setComponentEnabledSetting(
                componentName,
                PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
                PackageManager.DONT_KILL_APP);
    }

    /**
     * 选择默认的启动图标
     *
     * @author fengzhen
     * @version v1.0, 2017/8/1 14:40
     */
    public void choseDefault(View view) {
        enableComponent(mDefault);
        disableComponent(mRed);
        disableComponent(mBlue);
    }

    /**
     * 选择红色的启动图标
     *
     * @author fengzhen
     * @version v1.0, 2017/8/1 14:40
     */
    public void choseRed(View view) {
        enableComponent(mRed);
        disableComponent(mDefault);
        disableComponent(mBlue);
    }

    /**
     * 选择蓝色的启动图标
     *
     * @author fengzhen
     * @version v1.0, 2017/8/1 14:40
     */
    public void choseBlue(View view) {
        enableComponent(mBlue);
        disableComponent(mDefault);
        disableComponent(mRed);
    }

4.简单的布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center"
    tools:context="com.hftsoft.changelaunchericon.MainActivity">

    <Button
        android:text="默认图标"
        android:onClick="choseDefault"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <Button
        android:text="红色图标"
        android:onClick="choseRed"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <Button
        android:text="蓝色图标"
        android:onClick="choseBlue"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>
展开阅读全文

没有更多推荐了,返回首页