主屏幕快捷方式到另一个应用程序

时间:2016-10-07 07:00:32

标签: android android-intent shortcut

我正在创建一个应用程序,如果用户安装了该应用程序,则为我的另一个应用程序制作主屏幕快捷方式。

部分工作。在API级别低于23它完美地工作。在android 6上它创建了快捷方式,但是绕过Intent.EXTRA_SHORTCUT_NAMEIntent.EXTRA_SHORTCUT_ICON_RESOURCE并留下原始图标和名称,我不想使用它。

以下是我正在使用的代码示例:

ApplicationInfo selectedApp; //app that should be used for shortcut
Intent shortcutIntent = new Intent(getPackageManager().getLaunchIntentForPackage(selectedApp.packageName));
shortcutIntent.setAction(Intent.ACTION_MAIN);
addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "MyNewShortcut");
addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(getApplicationContext(), R.drawable.ic1));
addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
addIntent.putExtra("duplicate", false);
getApplicationContext().sendBroadcast(addIntent);

清单:

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

我应该在 Android Marshmallow 上做些什么不同的事情?

修改

好的,这有点令人困惑。我设法让它以某种方式工作。
当我添加这一行时:

shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "asd");

它在主屏幕上创建了一个快捷方式,但我在addIntent上设置了名称,而在新行上没有设置"asd"。对此有任何逻辑解释吗?

1 个答案:

答案 0 :(得分:0)

Android M 似乎存在某种错误。

为了获得新图标和名称的快捷方式,我不得不为第一个意图添加额外的名称。它可能是一个空字符串,因为名称将保留第二个意图。现在这样工作:

ApplicationInfo selectedApp; //app that should be used for shortcut

Intent shortcutIntent = new Intent(getPackageManager().getLaunchIntentForPackage(selectedApp.packageName));
shortcutIntent.setAction(Intent.ACTION_MAIN);
shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "asd"); //EDIT additional string for first intent that fixes Android M

addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "MyNewShortcut");
addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(getApplicationContext(), R.drawable.ic1));
addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
addIntent.putExtra("duplicate", false);

getApplicationContext().sendBroadcast(addIntent);
相关问题