Principle:
In the manifest file, use the <activity-alias> tag to prepare multiple for our entry activity, with the <activity-alias> tag activity to point to the entry activity, Each activity with <activity-alias> tags can be set up with an icon individually, in which we dynamically set <ACTIVITY-ALIAS>, and then kill launcher, When the launcher restarts, Icon replaces the note:
Kill launcher need to add permission <uses-permission android:name= "Android.permission.KILL_BACKGROUND_PROCESSES"/> Code:
Manifest.xml:
[HTML]View Plaincopy
- <activity
- android:name="com.dance.changeicondemo.MainActivity"
- android:label="@string/app_name" >
- <intent-filter>
- <action android:name="Android.intent.action.MAIN" />
- </intent-filter>
- </Activity>
- <activity-alias
- android:name="COM.EXAMPLE.CHANGEICONDEMO.ACTIVITYALIAS1"
- android:enabled="true"
- android:icon="@drawable/ic_launcher"
- android:label="@string/app_name"
- android:targetactivity="com.dance.changeicondemo.MainActivity" >
- <intent-filter>
- <action android:name="Android.intent.action.MAIN" />
- <category android:name="Android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity-alias>
- <activity-alias
- android:name="Com.example.changeicondemo.ActivityAlias2"
- android:enabled="false"
- android:icon="@drawable/icon2"
- android:label="@string/app_name"
- android:targetactivity="com.dance.changeicondemo.MainActivity" >
- <intent-filter>
- <action android:name="Android.intent.action.MAIN" />
- <category android:name="Android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity-alias>
Code:
[Java]View Plaincopy
- Private void SetIcon (String activity_alias) {
- Context CTX = Myapplication.getcontext ();
- Packagemanager pm = Ctx.getpackagemanager ();
- Activitymanager am = (activitymanager) ctx.getsystemservice (Activity.activity_service);
- //enable/disable activity-aliases
- Pm.setcomponentenabledsetting (
- New ComponentName (CTX, activity_alias_1),
- Activity_alias_1.equals (Activity_alias)? Packagemanager.component_enabled_state_enabled
- : packagemanager.component_enabled_state_disabled,
- Packagemanager.dont_kill_app);
- Pm.setcomponentenabledsetting (
- New ComponentName (CTX, activity_alias_2),
- Activity_alias_2.equals (Activity_alias)? Packagemanager.component_enabled_state_enabled
- : packagemanager.component_enabled_state_disabled,
- Packagemanager.dont_kill_app);
- //Find launcher and kill it
- 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);
- }
- }
- }
Android dynamic Edit App Desktop icon