简体   繁体   English

通过代码更改应用程序图标-android studio运行不再起作用

[英]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... 问题是,android studio run始终会尝试启动我在设备上更改的默认别名...因此它找不到默认活动,因为此活动已停用...

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. 我知道我可以在android studio run config中定义一个活动,但是实际上我希望有一个配置可以启动设备上当前处于活动状态的任何别名。 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.setClassName(“ your.package.name”,“ YourLauncherActivityName”); myLauncherIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); myLauncherIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

Intent intent = new Intent();

intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, myLauncherIntent); intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT,myLauncherIntent); intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "Application Name"); intent.putExtra(Intent.EXTRA_SHORTCUT_NAME,“应用程序名称”); intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,Intent.ShortcutIconResource.fromContext(getApplicationContext(),R.drawable.app_icon)); intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,Intent.ShortcutIconResource.fromContext(getApplicationContext(),R.drawable.app_icon)); intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT"); intent.setAction(“ com.android.launcher.action.INSTALL_SHORTCUT”); getApplicationContext().sendBroadcast(intent); getApplicationContext()。sendBroadcast(intent);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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