android动态修改app桌面icon

原理:
在Manifest文件中,使用<activity-alias>标签为我们的入口Activity准备多个,拥有<activity-alias>标签的activity是为了指向入口Activity,每个拥有<activity-alias>标签的activity都可以单独设置一个icon,在程序中我们动态设置<activity-alias>,然后kill掉launcher,等launcher重启后,icon就替换了

注意:
kill掉launcher需加权限 <uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES"/>

代码:
Manifest.xml:

[html] view plaincopy
 
  1. <activity  
  2.             android:name="com.dance.changeicondemo.MainActivity"  
  3.             android:label="@string/app_name" >  
  4.             <intent-filter>  
  5.                 <action android:name="android.intent.action.MAIN" />  
  6.             </intent-filter>  
  7.         </activity>  
  8.         <activity-alias  
  9.             android:name="com.example.changeicondemo.ActivityAlias1"  
  10.             android:enabled="true"  
  11.             android:icon="@drawable/ic_launcher"  
  12.             android:label="@string/app_name"  
  13.             android:targetActivity="com.dance.changeicondemo.MainActivity" >  
  14.             <intent-filter>  
  15.                 <action android:name="android.intent.action.MAIN" />  
  16.                 <category android:name="android.intent.category.LAUNCHER" />  
  17.             </intent-filter>  
  18.         </activity-alias>  
  19.         <activity-alias  
  20.             android:name="com.example.changeicondemo.ActivityAlias2"  
  21.             android:enabled="false"  
  22.             android:icon="@drawable/icon2"  
  23.             android:label="@string/app_name"  
  24.             android:targetActivity="com.dance.changeicondemo.MainActivity" >  
  25.             <intent-filter>  
  26.                 <action android:name="android.intent.action.MAIN" />  
  27.                 <category android:name="android.intent.category.LAUNCHER" />  
  28.             </intent-filter>  
  29.         </activity-alias>  

code:

[java] view plaincopy
 
  1. private void setIcon(String activity_alias) {  
  2.         Context ctx = MyApplication.getContext();  
  3.         PackageManager pm = ctx.getPackageManager();  
  4.         ActivityManager am = (ActivityManager) ctx.getSystemService(Activity.ACTIVITY_SERVICE);  
  5.    
  6.         // Enable/disable activity-aliases  
  7.         pm.setComponentEnabledSetting(  
  8.                 new ComponentName(ctx, ACTIVITY_ALIAS_1),  
  9.                 ACTIVITY_ALIAS_1.equals(activity_alias) ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED  
  10.                         : PackageManager.COMPONENT_ENABLED_STATE_DISABLED,  
  11.                 PackageManager.DONT_KILL_APP);  
  12.         pm.setComponentEnabledSetting(  
  13.                 new ComponentName(ctx, ACTIVITY_ALIAS_2),  
  14.                 ACTIVITY_ALIAS_2.equals(activity_alias) ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED  
  15.                         : PackageManager.COMPONENT_ENABLED_STATE_DISABLED,  
  16.                 PackageManager.DONT_KILL_APP);  
  17.    
  18.         // Find launcher and kill it  
  19.         Intent i = new Intent(Intent.ACTION_MAIN);  
  20.         i.addCategory(Intent.CATEGORY_HOME);  
  21.         i.addCategory(Intent.CATEGORY_DEFAULT);  
  22.         List<ResolveInfo> resolves = pm.queryIntentActivities(i, 0);  
  23.         for (ResolveInfo res : resolves) {  
  24.             if (res.activityInfo != null) {  
  25.                 am.killBackgroundProcesses(res.activityInfo.packageName);  
  26.             }  
  27.         }  
  28.     }  

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。