Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 7.1 快捷方式 Shortcuts 完全使用指南

編輯:關於 Android 程式設計

长按桌面图标弹出的那排快捷入口,就是 Android 7.1 引入的 Shortcuts 功能。它允许用户在不打开应用的情况下直达常用操作。

官方文档:https://developer.android.com/preview/shortcuts.html

相关开源整理可参考:https://github.com/ddwhan0123/Useful-Open-Source-Android

两种加载方式

  • 静态加载:写在 XML 里,预先定义好 UI 与跳转逻辑,兼容性更好。
  • 动态加载:运行时通过 API 添加或更新,灵活但仅 7.1 及以上支持。

Static Shortcuts

在 AndroidManifest.xml 中,为首页 Activity 的 intent-filter 之后添加 meta-data:

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

然后在 res/xml/shortcuts.xml 中定义具体条目,类似预设 Menu 的概念:

<shortcuts xmlns:android="https://schemas.android.com/apk/res/android">

    <shortcut
        android:enabled="true"
        android:icon="@drawable/happy"
        android:shortcutDisabledMessage="@string/one_disabled"
        android:shortcutId="shortcut1"
        android:shortcutLongLabel="@string/one_long"
        android:shortcutShortLabel="@string/one_text">
        <intent
            android:action="one"
            android:targetClass="demo.wjj.shortcutsdemo.MainActivity"
            android:targetPackage="demo.wjj.shortcutsdemo" />
    </shortcut>

    <shortcut
        android:enabled="false"
        android:icon="@drawable/woman"
        android:shortcutDisabledMessage="@string/two_disabled"
        android:shortcutId="shortcut2"
        android:shortcutLongLabel="@string/two_long"
        android:shortcutShortLabel="@string/two">
        <intent
            android:action="two"
            android:targetClass="demo.wjj.shortcutsdemo.MainActivity"
            android:targetPackage="demo.wjj.shortcutsdemo" />
    </shortcut>

    <shortcut
        android:enabled="true"
        android:icon="@drawable/angry"
        android:shortcutDisabledMessage="@string/three_disabled"
        android:shortcutId="shortcut3"
        android:shortcutLongLabel="@string/three_long"
        android:shortcutShortLabel="@string/three">
        <intent
            android:action="three"
            android:targetClass="demo.wjj.shortcutsdemo.MainActivity"
            android:targetPackage="demo.wjj.shortcutsdemo" />
    </shortcut>

</shortcuts>

包名、类名、图标、文案按需替换。几个属性值得留意:

  • android:action 是回到 Activity 后用于判断来源的 key,在业务界面通过 getIntent().getAction() 区分即可。
  • android:enabled 设为 false 时该条目不显示,示例中第二个 item 就是因为这个原因没出现。
  • android:shortcutDisabledMessage 是该条目被禁用时向用户展示的说明文案。
  • shortcutId 是后续动态更新时的唯一标识。

Dynamic Shortcuts

动态方式代码更灵活,官方提供的主要方法有:

setDynamicShortcuts(List)       // 重新设置动态快捷方式列表
addDynamicShortcuts(List)       // 追加到已有列表
updateShortcuts(List)           // 更新指定条目
removeDynamicShortcuts(List)    // 移除指定条目
removeAllDynamicShortcuts()     // 移除全部

官方给出的跳转网页示例:

ShortcutManager shortcutManager = getSystemService(ShortcutManager.class);
ShortcutInfo shortcut = new ShortcutInfo.Builder(this, "id1")
        .setShortLabel("Web site")
        .setLongLabel("Open the web site")
        .setIcon(Icon.createWithResource(context, R.drawable.icon_website))
        .setIntent(new Intent(Intent.ACTION_VIEW,
                Uri.parse("https://www.mysite.example.com/")))
        .build();
shortcutManager.setDynamicShortcuts(Arrays.asList(shortcut));

设计规范要点

  • 遵循 Material Design 图标设计指南:https://material.google.com/style/icons.html
  • 最多发布四个不同快捷方式。系统上限是五个,但过多会让长按菜单显得臃肿。
  • 控制文案长度。建议「简短描述」不超过 10 个字符,「长说明」不超过 25 个字符。
  • 保持快捷方式与实际功能的对应关系,确保用户点击后能完成应用内同样可完成的任务。
  • 只在含义保持不变时更新快捷方式,避免用户认知混乱。
  • 动态快捷方式不会在备份与恢复过程中保留。

调试环境的坑

写完之后最容易卡住的一步是:没有 7.1 设备,demo 跑起来看不到任何效果。系统自带启动器如果不支持 Shortcuts,长按图标是不会弹出菜单的。

解决办法是换一个支持该特性的第三方启动器,比如 Nova Launcher,官网在 https://www.apkmirror.com/apk/teslacoil-software/nova-launcher/ 。装上之后无需任何额外配置,长按图标就能看到自带的快捷方式,自己的 demo 也会正常显示。

补充一点:官方其实提供了完整 sample,可以直接参考 https://developer.android.com/samples/AppShortcuts/project.html 。

示例代码与 apk:https://github.com/ddwhan0123/BlogSample/tree/master/ShortcutsDemo

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved