Android定义可在自定义启动器中使用的快捷方式 [英] Android define shortcut that can be used in custom launcher

查看:101
本文介绍了Android定义可在自定义启动器中使用的快捷方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Nova Launcher(以及许多其他自定义启动器)中,有一个Gesture配置,您可以在其中为手势分配各种动作.在这些操作中,有一个快捷方式"选项卡.如何在自己的应用程序中添加可以被启动器识别的快捷方式? (我想这是我必须添加到清单中并为其设置Intent的东西)

In Nova Launcher (and many other custom ones) there is a Gesture configuration where you can assign various actions to a gesture. Among those actions there is a Shortcuts tab. How can I add a shortcut in my own app that can be recognized by the launcher? (I suppose it's something that I have to add to the Manifest and set an Intent to it)

这是一个

Here's an image to point out those actions.

推荐答案

这些文档的文献很少,不幸的是以前被称为"Launcher快捷方式",现在已经无名.除了位于Nova的手势菜单等之外,它们还显示在小部件抽屉中的小部件旁边.我认为这是小工具快捷方式".

These are very poorly documented and unfortunately were previously called "Launcher shortcuts" and now have been left nameless. In addition to being in Nova's gesture menus and similar, they also show next to widgets in the widget drawer. I think of the as "widget shortcuts".

官方文档似乎仅限于ApiDemos中的演示: https ://android.googlesource.com/platform/development/+/master/samples/ApiDemos/src/com/example/android/apis/app/LauncherShortcuts.java

The official documentation seems to be limited to a demo in ApiDemos: https://android.googlesource.com/platform/development/+/master/samples/ApiDemos/src/com/example/android/apis/app/LauncherShortcuts.java

这个想法是,在清单中处理android.intent.action.CREATE_SHORTCUT,并将带有标签,图标和目标意图的结果返回到启动器.然后,启动器可以将其设置为桌面上的图标,或者仅使用意图进行手势或其他操作.

The idea is that in your manifest you handle android.intent.action.CREATE_SHORTCUT and return a result to the launcher with the label, icon and target intent. Then the launcher can make it into an icon on the desktop or just use the intent for a gesture or whatever.

在您的AndroidManifest.xml

<activity android:name=".CreateShortcut">
    <intent-filter>
        <action android:name="android.intent.action.CREATE_SHORTCUT" />
            <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</activity>

然后,当用户选择快捷方式时,启动程序将对该活动进行startActivityForResult,您可以立即finish并显示结果,也可以向用户显示UI以让他们配置快捷方式. 无论哪种方式,当准备就绪时,都将信息返回到启动器:

Then when the user picks your shortcut the launcher does a startActivityForResult of that activity, you can either finish right away with a result, or you can display a UI to the user to let them configure the shortcut. Either way, when ready return the info to the launcher:

Intent result = new Intent();
result.putExtra(Intent.EXTRA_SHORTCUT_INTENT, targetIntent);
result.putExtra(Intent.EXTRA_SHORTCUT_NAME, targetLabel);
Parcelable iconResource = Intent.ShortcutIconResource.fromContext(
            this,  R.drawable.ic_launcher_shortcut);
result.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconResource);

setResult(RESULT_OK, result);
finish();

这篇关于Android定义可在自定义启动器中使用的快捷方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆