繁体   English   中英

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

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

如果用户希望更改我的应用程序图标,我可以使用如下功能:

 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);
}

问题是,android studio run始终会尝试启动我在设备上更改的默认别名...因此它找不到默认活动,因为此活动已停用...

我知道我可以在android studio run config中定义一个活动,但是实际上我希望有一个配置可以启动设备上当前处于活动状态的任何别名。 有可能吗? 诸如“检查清单并尝试一个接一个的启动器活动,直到一个启动为止”之类的东西……

在清单中添加权限:

<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,“应用程序名称”); 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);

暂无
暂无

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

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