Android 动态更换桌面图标

每当双 11、12 来临之际,Android 手机 Launcher 中的淘宝、天猫图标就会变成双 11、12 主题的图标。实现了动态切换图标、名称

MainActivity

package com.example.modifyappdemo;

import android.app.Activity;
import android.app.ActivityManager;
import android.content.ComponentName;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

import static android.content.pm.PackageManager.COMPONENT_ENABLED_STATE_DISABLED;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private Button changeIcon;
    private ActivityManager activityManager;
    private PackageManager packageManager;
    private boolean setIcon = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        activityManager = (ActivityManager) this.getSystemService(Activity.ACTIVITY_SERVICE);
        packageManager = getPackageManager();
        changeIcon = findViewById(R.id.changeIcon);
        changeIcon.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.changeIcon:
                if ("SecondApp".equals(getTitle())) {
                    setIcon = false;
                } else {
                    setIcon = true;
                }

                packageManager.setComponentEnabledSetting(new ComponentName(this, "com.example.modifyappdemo.MainActivity"),
                        setIcon == true ? COMPONENT_ENABLED_STATE_DISABLED : PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
                        PackageManager.DONT_KILL_APP);

                packageManager.setComponentEnabledSetting(new ComponentName(this, "com.example.modifyappdemo.changeAfter"),
                        setIcon == true ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED : PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
                        PackageManager.DONT_KILL_APP);
                break;
            default:
                break;
        }
    }
}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.modifyappdemo">
<uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES"/>
    <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:roundIcon="@mipmap/ic_launcher_round"
            android:supportsRtl="true"
            android:theme="@style/AppTheme">
        <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:icon="@mipmap/alias_icon"
                android:label="@string/app_name2"
                android:name="changeAfter"
                android:targetActivity=".MainActivity"
                android:enabled="false">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

    </application>

</manifest>

strings.xml

<resources>
    <string name="app_name">FirstApp</string>
    <string name="app_name2">SecondApp</string>
</resources>

 

posted on 2019-10-19 12:25  LoaderMan  阅读(3535)  评论(0编辑  收藏  举报

导航