Android 動態更換桌面圖標

每當雙 十一、12 來臨之際,Android 手機 Launcher 中的淘寶、天貓圖標就會變成雙 十一、12 主題的圖標。實現了動態切換圖標、名稱android

MainActivityapp

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.xmlide

<?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.xmlthis

<resources>
    <string name="app_name">FirstApp</string>
    <string name="app_name2">SecondApp</string>
</resources>
相關文章
相關標籤/搜索