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

Home screen shortcut to another application

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

部分有效。在 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"。有什么逻辑解释吗?

似乎 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);