Android|Android app的多个快捷方式
多个入口进入MainActivity.java中,需要在清单文件中定义activity-alias属性;
【Android|Android app的多个快捷方式】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 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;
}}
}
推荐阅读
- 热闹中的孤独
- JAVA(抽象类与接口的区别&重载与重写&内存泄漏)
- 宽容谁
- 放屁有这三个特征的,请注意啦!这说明你的身体毒素太多
- 一个人的旅行,三亚
- 布丽吉特,人生绝对的赢家
- 慢慢的美丽
- 尽力
- 一个小故事,我的思考。
- 家乡的那条小河