如何创建导致非启动器活动的快捷方式?

发布于 2024-10-11 23:27:12 字数 53 浏览 6 评论 0原文

我想在 Android 应用程序中创建一个快捷方式,它会导致另一个不是应用程序启动器的活动。

I want to create a shortcut in an android app, it lead to another activity which is not launcher of the app.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

雨落星ぅ辰 2024-10-18 23:27:12

要创建快捷方式本身,您需要一个特制的活动,该活动必须:

  • 在 AndroidManifest.xml 中使用意图过滤器和操作 android.intent.action.CREATE_SHORTCUT 进行定义。
  • 返回一个结果,一个 Intent,包含您的实际快捷方式。快捷方式本身由另一个 Intent 表示。

当您长按桌面并选择“快捷方式”时,就会显示此活动。

当然,快捷方式本身并没有多大用处,因此您必须将意图过滤器添加到您想要由快捷方式触发的任何活动中。意图过滤器应与您为快捷方式选择的任何意图相匹配。

我写了一个关于这个主题的小指南,它有更多细节:http://www.kind-kristiansen.no/2010/android-adding-desktop-shortcut-support-to-your-app/

如果有任何不清楚的地方请告诉我那个帖子,我会尽力清理它。

To create the shortcut itself you need a specially crafted activity which must:

  • Be defined in your AndroidManifest.xml with an intent filter with the action android.intent.action.CREATE_SHORTCUT.
  • Return a result, an Intent, containing your actual shortcut. The shortcut itself is represented by another Intent.

This activity will then show up when you longpress your desktop and select "Shortcuts".

Of course the shortcut by itself is not much use, so you must add an intent filter to whatever activity you want to get triggered by the shortcut. The intent filter should match whatever Intent you chose for your shortcut.

I wrote a small how-to on the subject, it's got more details: http://www.kind-kristiansen.no/2010/android-adding-desktop-shortcut-support-to-your-app/

Do tell me if anything is unclear in that post, I'll try to clear it up.

黑白记忆 2024-10-18 23:27:12

我开发了以下一种方法来在 Android 主屏幕上创建快捷方式图标。只要调用它即可。

private void ShortcutIcon(){

    Intent shortcutIntent = new Intent(getApplicationContext(), MainActivity.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, "Test");
    addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(getApplicationContext(), R.drawable.ic_launcher));
    addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
    getApplicationContext().sendBroadcast(addIntent);
}

不要忘记更改您的活动名称、图标资源。快乐编码!

I have developed one method below for creating the shortcut icon on android Homescreen. Just call it.

private void ShortcutIcon(){

    Intent shortcutIntent = new Intent(getApplicationContext(), MainActivity.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, "Test");
    addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(getApplicationContext(), R.drawable.ic_launcher));
    addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
    getApplicationContext().sendBroadcast(addIntent);
}

Don't forget to change your activity name, icon resource . Happy coding !!!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文