多个入口进入MainActivity.java中,需要在清单文件中定义activity-alias属性;
<!-- 杀掉launcher所需权限 -->
<uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity-alias
android:name="com.example.dynamicchangeicon.ActivityAlias1"
android:enabled="false"
android:icon="@drawable/hasicon"
android:label="@string/app_name"
android:targetActivity="com.example.dynamicchangeicon.MainActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity-alias>
<activity-alias
android:name="com.example.dynamicchangeicon.TestAlias"
android:enabled="false"
android:icon="@drawable/icon"
android:label="@string/app_name"
android:targetActivity="com.example.dynamicchangeicon.MainActivity" >
<intent-filter>
<category android:name="android.intent.category.DEFAULT" />
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity-alias>
</application>
Activity中的代码:
public class MainActivity extends Activity {
private final String ACTIVITY_ALIAS_1 = "com.example.dynamicchangeicon.ActivityAlias1";
private final String ACTIVITY_ALIAS_2 = "com.example.dynamicchangeicon.TestAlias";
private SharedPreferences preferences;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
preferences = getSharedPreferences("DynamicChangeIcon",
Context.MODE_PRIVATE);
preferences.edit().putInt("ikey", 1).commit();
}
private void changeIcon(String msg) {
//msg 就是在清单文件给activity-alias定制的name属性:
// android:name="com.example.dynamicchangeicon.ActivityAlias1"
PackageManager pm = getApplicationContext().getPackageManager();
ActivityManager am = (ActivityManager) getSystemService(Activity.ACTIVITY_SERVICE);
System.out.println(getComponentName());
// 去除旧图标,不去除的话会出现2个App图标
pm.setComponentEnabledSetting(getComponentName(),
PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP);
if(msg.equals(ACTIVITY_ALIAS_1)){
// 显示新图标
pm.setComponentEnabledSetting(new ComponentName(getBaseContext(),
ACTIVITY_ALIAS_1),
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP);
pm.setComponentEnabledSetting(new ComponentName(getBaseContext(),
ACTIVITY_ALIAS_2),
PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP);
}else{
pm.setComponentEnabledSetting(new ComponentName(getBaseContext(),
ACTIVITY_ALIAS_2),
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP);
pm.setComponentEnabledSetting(new ComponentName(getBaseContext(),
ACTIVITY_ALIAS_1),
PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP);
}
Intent i = new Intent(Intent.ACTION_MAIN);
i.addCategory(Intent.CATEGORY_HOME);
i.addCategory(Intent.CATEGORY_DEFAULT);
List<ResolveInfo> resolves = pm.queryIntentActivities(i, 0);
for (ResolveInfo res : resolves) {
if (res.activityInfo != null) {
am.killBackgroundProcesses(res.activityInfo.packageName);
}
}
}
public void changeIcon(View v) {
int key = preferences.getInt("ikey", 0);
switch (key) {
case 1:
changeIcon(ACTIVITY_ALIAS_1);
preferences.edit().putInt("ikey", 2).commit();
break;
case 2:
changeIcon(ACTIVITY_ALIAS_2);
preferences.edit().putInt("ikey", 1).commit();
break;
}
}
}