Android 动态更换桌面图标
Posted 码上加油站
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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>
以上是关于Android 动态更换桌面图标的主要内容,如果未能解决你的问题,请参考以下文章
求助~~有啥办法可以更换LauncherPro桌面上的图标吗?
怎样实现android Application的icon图标动态变化?