Android定义可在自定义启动器中使用的快捷方式

Ale*_*man 3 android

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

编辑:这是一个指出这些行动的图像.

Kev*_*oil 8

这些记录非常糟糕,不幸的是之前被称为"Launcher快捷方式",现在已经无名了.除了在Nova的手势菜单和类似之外,它们还显示在小部件抽屉中的小部件旁边.我认为是"小部件快捷方式".

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

我们的想法是在您的清单中处理android.intent.action.CREATE_SHORTCUT并使用标签,图标和目标意图将结果返回给启动器.然后,启动器可以将其变为桌面上的图标,或者只是使用手势或其他任何内容.

在你的 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>
Run Code Online (Sandbox Code Playgroud)

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

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();
Run Code Online (Sandbox Code Playgroud)