简体   繁体   中英

Change app icon by code - android studio run does not work anymore

I use a function like this to change my apps icon if the user wishes to do so:

 public static void changeIcon(Activity activity, String addon)
{
    // 1) adactivate all aliases
    List<String> names = AppIconStyle.getNames();
    for (int i = 0; i < AppIconStyle.getNames().size(); i++)
    {
        ComponentName componentNameToDeactivate = new ComponentName(activity.getPackageName(), MainActivityMVP.class.getName() + "-" + names.get(i));
        activity.getPackageManager().setComponentEnabledSetting(componentNameToDeactivate, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
    }
    // 2) activate selected alias
    ComponentName componentNameToActivate = new ComponentName(activity.getPackageName(), MainActivityMVP.class.getName() + "-" + addon);
    activity.getPackageManager().setComponentEnabledSetting(componentNameToActivate, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
}

The problem is, android studio run does always try to start the default alias which I changed on my device... So it does not find the default activity, because this one is deactivated...

Question

I know I can define an activity in android studio run config, but actually I would like to have one config that starts whichever alias currently is active on my device. Is that somehow possible? Something like "check the manifest and try to start one launcher activity after another until one works" or so...

Add permissions in manifest :

<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />

Intent myLauncherIntent = new Intent();

myLauncherIntent.setClassName("your.package.name", "YourLauncherActivityName"); myLauncherIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

Intent intent = new Intent();

intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, myLauncherIntent); intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "Application Name"); intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,Intent.ShortcutIconResource.fromContext(getApplicationContext(),R.drawable.app_icon)); intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT"); getApplicationContext().sendBroadcast(intent);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM