【发布时间】:2017-03-21 06:08:44
【问题描述】:
这个问题听起来可能是重复的,但我没有找到有效的答案:
我已经解决了这些问题:
Android create shortcuts on the home screen
但建议的解决方案不起作用。
我使用了以下在 API 级别 中工作的解决方案
Intent shortcutIntent = new Intent(context,
LedgerDetailActivity.class);
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Intent addIntent = new Intent();
addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, ledgerBean.getName());
addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(context, R.drawable.ic_action_comments));
addIntent.setAction("android.intent.action.CREATE_SHORTCUT");
addIntent.putExtra("duplicate", false);
context.sendBroadcast(addIntent);
添加权限:
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
但上述解决方案并非适用于所有设备,并且有时会提供如下奇怪的功能:
- 在某些设备中,例如 Samsung Edge(Android N)无法创建快捷方式
- 在我的 Android 7.1 (Android N) 模拟器中,只创建了一个快捷方式
谁能帮帮我,这个功能没有官方文档,请不要与Android N中引入的App快捷方式混淆。我需要主屏幕的快捷方式。
【问题讨论】:
-
不要从所有不起作用的事情开始,也许您应该从解释您正在尝试做什么开始。仅凭主题是不够的。至少有一个答案完全误解了你。您可能需要提供屏幕截图并解释预期结果。
-
操作应该是
com.android.launcher.action.INSTALL_SHORTCUT。在我测试过的三星设备上,创建快捷方式对我也不起作用。 -
更新我上面的评论。我使用的是
Intent.EXTRA_SHORTCUT_ICON和bitmap。这导致了TransactionTooLargeException。我不得不调整位图的大小。
标签: android android-launcher android-shortcut