Android 在Launcher桌面添加应用快捷图标AppShortcuts/PinnedShortcuts(适用于Android 7.1(API25) 及更高)

如果你的应用的目标是Android 7.1(API级别25)或更高,则可以在应用中定义特定操作的快捷方式。这些快捷方式可以显示在支持的启动器中。快捷方式可让您的用户在应用内快速启动常用或推荐的任务。每个快捷方式引用一个或多个Intent,当用户选择快捷方式时,每个Intent都将在您的应用程序中启动特定的操作。

添加应用快捷方式/快捷键:

目前支持两种类型快捷方式,一类是通过长按应用图标弹出的App shortcuts,一类是添加到桌面中显示的Pinned shortcuts(固定快捷键),App shortcuts支持动态添加,更新,删除,禁用,Pinned shortcuts仅支持动态添加,更新,但无法通过程序移除,只能禁用。

App shortcuts:

静态快捷方式在打包到APK中的资源文件中定义。因此,您必须等到更新整个应用程序才能更改这些静态快捷方式的详细信息。
动态快捷方式在运行时使用 ShortcutManagerAPI发布。在运行时期间,您的应用可以发布,更新和删除其动态快捷方式。

Pinned shortcuts:

固定快捷方式在运行时发布,也使用
ShortcutManagerAPI。在运行时期间,您的应用可以尝试固定快捷方式,此时用户会收到一个确认对话框,要求他们允许固定快捷方式。固定的快捷方式仅在用户接受固定请求时出现在受支持的启动器中。

注意:用户也可以通过将应用程序的静态和动态快捷方式复制到启动器本身来创建固定快捷方式。每个应用最多发布五个快捷方式(静态快捷方式和动态快捷方式组合)。但是,某些启动器应用程序不会显示您为应用程序创建的每个静态和动态快捷方式。固定快捷方式创建的的数量没有限制。应用程序无法删除已固定的快捷方式,但可以禁用这些固定快捷方式。

应用程序快捷方式示例如下:
这里写图片描述

一、创建App shortcuts

1.1 静态快捷方式(Using Static Shortcuts)

静态快捷方式应提供应用程序中通用操作的链接,这些操作应在应用程序当前版本的整个生命周期内保持一致。静态快捷方式的好候选者包括查看发送的消息,设置闹钟以及显示用户当天的锻炼活动。长按应用图标在弹出的App shortcuts中显示。

1.首先在应用的AndroidManifest.xml中找到应用入口activity(其意图过滤器设置为 android.intent.action.MAIN 动作和 android.intent.category.LAUNCHER 类别)
2.向这个activty标签中添加meta-data元素,该元素引用定义应用快捷方式的资源文件:

        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>

            <meta-data
                android:name="android.app.shortcuts"
                android:resource="@xml/shortcuts"/>
        </activity>

3.创建一个新的资源文件:res/xml/shortcuts.xml
在这个新的资源文件中,添加一个根元素,其中包含元素列表。每个元素又包含有关静态快捷方式的信息,包括其图标,其描述标签以及它在应用内启动的意图:

<?xml version="1.0" encoding="utf-8"?>
<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
    <shortcut
        android:shortcutId="compose"
        android:enabled="true"
        android:icon="@drawable/logo"
        android:shortcutShortLabel="@string/compose_shortcut_short_label"
        android:shortcutLongLabel="@string/compose_shortcut_Long_label"
        android:shortcutDisabledMessage="@string/compose_disabled_message">
        <intent
            android:action="android.intent.action.VIEW"
            android:targetPackage="com.makesky.studydemo01"
            android:targetClass="com.makesky.studydemo01.AppShortcuts.AppShortcutsDemoActivity"/>
            <!-- 如果你的快捷方式与多个意图相关联,请将它们添加到这里,列表中的最后一个意图决定了用户最先看到的内容。
             如果你的快捷方式与多个意图相关联,系统将在资源文件中启动与该快捷方式的最后一个意图相对应的活动,
             并在后方堆栈中执行其他活动 。在这种情况下,当用户选择快捷方式然后按下后退键时,您的应用程序将启
             动与资源文件中列出的快捷方式的倒数第二个意向对应的活动。这种行为模式会一直重复按下后退按钮,直
             到用户清除快捷方式创建的后台堆栈。当用户下一次按下后退按钮时,系统将其导航回启动器。-->
        <categories android:name="android.shortcut.conversation"/>
    </shortcut>
    <!--指定更多的快捷键-->
</shortcuts>

4.安装到android7.1以上的系统中 长按应用图标 效果如下所示:
这里写图片描述

1.2 动态快捷方式:

动态快捷方式应提供指向您的应用中特定的,上下文相关的操作的链接。这些操作可能会在应用程序的使用之间发生变化,即使在您的应用程序运行时它们也可能会发生更改 动态快捷方式的好候选者包括调用特定人员,导航到特定位置以及查看特定游戏的当前分数。

ShortcutManager API可以在动态快捷方式上完成以下操作:

Action Intro
Publish(发布) 使用 setDynamicShortcuts() 重新定义动态快捷键的完整列表,或者使用 addDynamicShortcuts() 以增加动态快捷键的现有列表。
Update(更新) 使用该 updateShortcuts() 方法。
delete (删除) 使用删除一组动态快捷方式removeDynamicShortcuts(),或使用 删除所有动态快捷方式 removeAllDynamicShortcuts()。

a.添加动态快捷键

    /**
     * 添加动态快捷键
     */
    @TargetApi(Build.VERSION_CODES.N_MR1)
    private void addDynamicShortcuts() {
        ShortcutManager shortcutManager = getSystemService(ShortcutManager.class);

        ShortcutInfo shortcutInfo = new ShortcutInfo.Builder(this, "id1")
                .setShortLabel("S" + System.currentTimeMillis())
                .setLongLabel("L" + System.currentTimeMillis())
                .setIcon(Icon.createWithResource(this, R.drawable.img01))
                .setIntent(new Intent(this, SQLiteDemoActivity.class).setAction(Intent.ACTION_VIEW))//intent's action must be set
                .build();
        shortcutManager.setDynamicShortcuts(Arrays.asList(shortcutInfo));
    }

这里写图片描述

b.更新动态快捷键

    /**
     * 更新动态快捷键
     */
    @TargetApi(Build.VERSION_CODES.N_MR1)
    private void updateDynamicShortcuts() {
        ShortcutManager shortcutManager = getSystemService(ShortcutManager.class);

        List<ShortcutInfo> shortcutInfoList = shortcutManager.getDynamicShortcuts();
        for (ShortcutInfo info : shortcutInfoList) {
            if (info.getId().equals("id1")) {
                info = new ShortcutInfo.Builder(this, "id1")
                        .setShortLabel("S" + System.currentTimeMillis())
                        .setLongLabel("L" + System.currentTimeMillis())
                        .setIcon(Icon.createWithResource(this, R.drawable.img01))
                        .setIntent(new Intent(this, SQLiteDemoActivity.class).setAction(Intent.ACTION_VIEW))//intent's action must be set
                        .build();
                shortcutManager.updateShortcuts(Arrays.asList(info));
            }
        }
    }

这里写图片描述

c.更新动态快捷键

    /**
     * 删除动态快捷键
     */
    @TargetApi(Build.VERSION_CODES.N_MR1)
    private void deleteDynamicShortcuts() {
        ShortcutManager shortcutManager = getSystemService(ShortcutManager.class);

        List<ShortcutInfo> shortcutInfoList = shortcutManager.getDynamicShortcuts();
        for (ShortcutInfo info : shortcutInfoList) {
            if (info.getId().equals("id1")) {
                List<String> list = new ArrayList<>();
                list.add("id1");
                shortcutManager.removeDynamicShortcuts(list);
            }
        }
    }

这里写图片描述

注意:移除应用程序快捷方式后,固定快捷方式还在
这里写图片描述

二、创建Pinned shortcuts

在Android 8.0(API级别26)及更高版本上,您可以创建固定快捷方式。与静态和动态快捷方式不同,固定快捷方式作为单独的图标显示在受支持的启动器中。下图显示了这两种快捷方式之间的区别。
这里写图片描述

当您尝试将快捷方式固定到受支持的启动器上时,用户会收到一个确认对话框,要求其允许固定快捷方式。如果用户不允许快捷方式被固定,则启动程序将取消该请求。

要使用您的应用将快捷方式固定到受支持的启动器,请完成以下一系列步骤:

1.使用 isRequestPinShortcutSupported()来验证设备的默认启动支持,程序快捷方式的牵制。
2.ShortcutInfo根据快捷方式是否已存在,以两种方式之一创建对象:

  • 如果快捷方式已存在,请创建一个 ShortcutInfo仅包含现有快捷方式标识的对象。系统会自动查找并锁定与快捷方式相关的所有其他信息。
  • 如果要固定新的快捷方式,请创建一个ShortcutInfo包含新快捷方式 的ID,意向和短标签的对象。

3.尝试通过调用将快捷方式固定到设备启动器 requestPinShortcut()。在此过程中,您可以传入一个 PendingIntent对象,该对象仅在快捷方式成功锁定时才会通知您的应用程序。

注意:如果用户不允许将快捷方式固定到启动器,则您的应用程序不会收到回叫。

固定快捷方式后,您的应用可以使用该updateShortcuts() 方法更新其内容 。

注意:
1.在启动器中显示应用快捷方式的菜单中,空间有限。如果可能,请将快捷方式的“简短描述”的长度限制为10个字符,并将“长描述”的长度限制为25个字符。
2.可以添加无限多个固定快捷方式

2.1 添加固定快捷方式

    /**
     * 添加固定快捷方式 可以添加多个,但如果仅添加一个的话建议每次添加前查看已添加的固定快捷键列表
     */
    @TargetApi(26)
    private void addPinnedShortcuts() {
        ShortcutManager shortcutManager = getSystemService(ShortcutManager.class);

        //查看已经加载的固定快捷方式
        List<ShortcutInfo> shortcutInfoList = shortcutManager.getPinnedShortcuts();
        for (ShortcutInfo info : shortcutInfoList) {
            LogUtil.i(TAG, "ShortcutInfo:" + info.getId());
        }

        if (shortcutManager.isRequestPinShortcutSupported()) {
            //假设已经有一个快捷方式,ID为“id1”。
            //必须启用快捷方式。
            ShortcutInfo pinShortcutInfo = new ShortcutInfo.Builder(this, "id1").build();

            //创建PendingIntent对象,除非你的应用需要被通知
            //用户允许被固定的快捷方式。注意,如果
            //固定操作失败,你的应用没有被通知。我们假设这是
            //app已经实现了一种名为createshortcutresultin帐篷()的方法
            //返回一个广播意图。
            Intent pinnedShortcutCallbackIntent = shortcutManager.createShortcutResultIntent(pinShortcutInfo);
            //配置意图,让你的应用的广播接收器得到
            //回调成功。
            PendingIntent successCallback = PendingIntent.getBroadcast(this, 0, pinnedShortcutCallbackIntent, 0);

            shortcutManager.requestPinShortcut(pinShortcutInfo, successCallback.getIntentSender());
        }
    }

这里写图片描述

这里写图片描述

2.2 禁用快捷键(适用于App shortcuts以及Pinned shortcuts)

由于应用或者用户可以将快捷方式固定到设备的启动器,因此这些固定的快捷方式可能会引导用户执行应用中已过期或不再存在的操作。要管理这种情况,可以禁用不希望用户通过调用来选择的disableShortcuts()快捷方式,该快捷方式会从静态和动态快捷方式列表中删除指定的快捷方式,并禁用这些快捷方式的任何固定副本。您也可以使用此方法的 重载版本来定义在用户尝试启动禁用的快捷方式时应显示的错误消息。

    /**
     * 禁用快捷方式(固定、动态、静态)
     */
    @TargetApi(26)
    private void deletePinnedShortcut(){
        ShortcutManager shortcutManager = getSystemService(ShortcutManager.class);

        //查看已经加载的固定快捷方式
        List<ShortcutInfo> shortcutInfoList = shortcutManager.getPinnedShortcuts();
        for (ShortcutInfo info : shortcutInfoList) {

            LogUtil.i(TAG, "ShortcutInfo:" + info.getId());
            if (info.getId().equals("id1")) {
                List<String> list = new ArrayList<>();
                list.add("id1");
                shortcutManager.disableShortcuts(list, "快捷方式已失效");
            }
        }
    }

这里写图片描述
这里写图片描述


猜你喜欢

转载自blog.csdn.net/makercloud/article/details/80842491