1. 程式人生 > >更換APP啟動圖示和名稱

更換APP啟動圖示和名稱

情景

類似於雙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>