多個入口進入MainActivity.java中鹦蠕,需要在清單文件中定義activity-alias屬性;
<!-- 殺掉launcher所需權(quán)限 -->
<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());
// 去除舊圖標,不去除的話會出現(xiàn)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;
}
}
}