Last active
May 22, 2018 13:34
-
-
Save ikew0ng/5541030 to your computer and use it in GitHub Desktop.
动态替换launcher icon
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- activity --> | |
<activity | |
android:name=".ui.WelcomeActivity" | |
android:label="@string/app_name" | |
android:windowSoftInputMode="adjustResize|stateHidden" > | |
<intent-filter> | |
<action android:name="android.intent.action.MAIN" /> | |
</intent-filter> | |
</activity> | |
<activity-alias | |
android:name=".ui.WelcomeActivity_Common" | |
android:enabled="true" | |
android:icon="@drawable/ic_launcher" | |
android:label="@string/app_name" | |
android:targetActivity=".ui.WelcomeActivity" > | |
<intent-filter> | |
<action android:name="android.intent.action.MAIN" /> | |
<category android:name="android.intent.category.LAUNCHER" /> | |
</intent-filter> | |
</activity-alias> | |
<activity-alias | |
android:name=".ui.WelcomeActivity_MX2" | |
android:enabled="false" | |
android:icon="@drawable/ic_launcher_mx2" | |
android:label="@string/app_name" | |
android:targetActivity=".ui.WelcomeActivity" > | |
<intent-filter> | |
<action android:name="android.intent.action.MAIN" /> | |
<category android:name="android.intent.category.LAUNCHER" /> | |
</intent-filter> | |
</activity-alias> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private void setIcon(String activity_alias) { | |
Context ctx = AppData.getContext(); | |
PackageManager pm = ctx.getPackageManager(); | |
ActivityManager am = (ActivityManager) ctx.getSystemService(Activity.ACTIVITY_SERVICE); | |
// Enable/disable activity-aliases | |
pm.setComponentEnabledSetting( | |
new ComponentName(ctx, ACTIVITY_ALIAS_COMMON), | |
ACTIVITY_ALIAS_COMMON.equals(activity_alias) ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED | |
: PackageManager.COMPONENT_ENABLED_STATE_DISABLED, | |
PackageManager.DONT_KILL_APP); | |
pm.setComponentEnabledSetting( | |
new ComponentName(ctx, ACTIVITY_ALIAS_MX2), | |
ACTIVITY_ALIAS_MX2.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); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment