【问题标题】:Home screen shortcut to another application另一个应用程序的主屏幕快捷方式
【发布时间】:2016-10-07 07:00:31
【问题描述】:

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

它部分工作。在低于 23 的 API 级别上,它可以完美运行。在 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
  • 是普通级别的权限,不危险,不需要运行时请求。在Manifest.xml中设置就足够了

标签: android android-intent shortcut


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-06-24
    • 1970-01-01
    • 2011-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多