【发布时间】:2015-06-14 13:23:58
【问题描述】:
我想为我的用户提供一个选项,以便在应用程序中创建指向特定页面的快捷方式。 我在 Whatsapp 上看到过类似的用法,当您长按聊天时,您可以为该特定聊天创建桌面快捷方式。
我已尝试查找有关此functionality 的一些文档,但无法使其正常工作。 这是我所拥有的:
不是启动器活动的活动(包括意图过滤器)
<activity android:name="com.my.example.pages.Topics"
android:parentActivityName="com.my.example.pages.Apps">
<intent-filter>
<action android:name="android.intent.action.CREATE_SHORTCUT"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
创建快捷方式功能
public void createShortcut(){
Intent shortcutIntent = new Intent("com.my.example.pages.Topics");
Intent.ShortcutIconResource iconResource = Intent.ShortcutIconResource.fromContext(getActivity(), R.drawable.app_logo);
// The result we are passing back from this activity
Intent intent = new Intent();
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "Shortcut Test");
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconResource);
getActivity().setResult(getActivity().RESULT_OK, intent);
getActivity().finish();
Toast.makeText(getActivity(),"Shortcut created",Toast.LENGTH_SHORT).show();
}
清单
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
我可能遗漏了一些东西,因为在调用该函数后我得到了 Toasts,但没有创建快捷方式,并且应用程序由于 finish() 方法而退出。
为了更清楚 - 我如何为非启动器活动创建快捷方式?
*我正在我的一个 viewpager 片段中运行代码。
【问题讨论】:
-
感谢回答的人,Whatsapp 应用程序做得非常好和顺利。作为一个用户,我觉得这个选项非常有用,甚至是必要的
-
这似乎可行,“主题”必须是启动器活动
-
您是否尝试了链接问题中显示的确切步骤?
-
@NanaGhartey 为什么它必须是启动器活动?您可以看到 Whatsapp 聊天的页面不是启动器活动,并且快捷方式直接指向聊天
标签: android android-intent shortcut intentfilter