Shortcuts


Shortcuts 介绍

Shortcuts 提供了APP常用操作快捷访问方式,适用版本 Android 7.1(api level >=25 )

使用方式

  • 静态 使用XML注册的方式

  • 动态添加 注意 api level >=25Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1

静态 Shortcuts(Static Shortcuts)

使用XML注册的方式

  1. AndroidManifest.xml 的 Main Launcher 对应的 Activity 内添加 meta-data meta-data name 为 android.app.shortcuts
<application
    ……>
    <activity android:name=".main.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>
</application>

其中android:resource 指定 shortcuts 的资源文件

  1. 建立资源文件:在res目录下建立 xml 文件夹并新建 shortcuts.xml 文件
<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
    <shortcut
        android:enabled="true"
        android:icon="@drawable/xxx"
        android:shortcutId="myId"
        android:shortcutDisabledMessage="@string/disabled"
        android:shortcutLongLabel="@string/myShortcutLonglabel"
        android:shortcutShortLabel="@string/myShortcutShortLabel">
        <intent
            android:action="android.intent.action.VIEW"
            android:targetClass="xxx.xxx.xxxActivity"
            android:targetPackage="xxx.xxx"/>
        <intent
            ……/>
    </shortcut>
    ……
</shortcuts>

shortcuts元素为根,可以包含 多个 shortcut元素,每个shortcut元素表示一个 shortcut.其中属性分别表示:

  • shortcutId表示 shortcut 唯一标识符,相同的 shortcutId 会被覆盖.必须字段.

  • shortcutShortLabel为将 shortcut 拖动到桌面时显示的名字,官方建议不超过 10 个字符,必须字段.

  • shortcutLongLabel为 shortcut 列表中每个 shortcut 的名字,不宜过长,如果过长或未设置默认会显示 ShortLabel,官方建议不超过25 个字符.可选字段.

  • icon为 shortcut 的 icon,在列表展示和拖动到桌面时显示需要,可选字段.

  • enabled表示 shortcut 是否可用,false 表示禁用.xml 中这个属性几乎没有被设置为 false 的实际场景,对于静态 Shortcuts,直接删除配置文件中对应的 Shortcut 即可,系统桌面会将已固定的该 Shortcut 置灰,点击会提示 shortcutDisabledMessage .对于动态 Shortcuts 建议通过禁用的方式而不是直接删除的方式,因为已经删除的动态 Shortcut 如果被固定了依然是可用的,所以希望该入口不可用最好的方式是禁用.

  • shortcutDisabledMessage为已固定在桌面的 shortcut 被 Disabled 后点击时的 Toast 提示内容.可选字段.

  • intent为点击 shortcut 时响应的 Intent,必须字段.这里可以添加多个 Intent,但点击时不会启动所有 Intent,而是启动最后一个 Intent,在这个 Intent 回退时会启动它前面一个 Intent,相当于自动将所有 Intent 添加到了堆栈对于先跳转到某个页面,Back 键希望退回主页而不是结束 App 这类场景,多个 Intents 挺实用的.

intent可设置属性包括:

android:action、android:data、android:mimeType、android:targetClass、android:targetPackage

其中android:action为必须属性.

动态 Shortcuts(Dynamic Shortcuts)

动态的shortcuts可以在用户使用app的过程中构建, 更新, 或者删除.

代码示例:


if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N_MR1) {
    return;
}

ShortcutManager shortcutManager = getSystemService(ShortcutManager.class);
ShortcutInfo shortcut = new ShortcutInfo.Builder(this"id")
    .setShortLabel("这是短标签")
    .setLongLabel("这是长标签")
    .setDisabledMessage("Disabled")
    .setIcon(Icon.createWithResource(context, R.drawable.xxx))
    //Intent 是必须设置的字段,并且Intent必须设置Action
    //多个Intent时,传递Intent[]数组.组成为back stack
    .setIntent(new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.google.com/")))
    .build();
shortcutManager.setDynamicShortcuts(Arrays.asList(shortcut));

使用ShortcutManager可以对动态Shortcuts完成下面几种操作:

  • Publish发布: setDynamicShortcuts() 以替换并添加所有 shortcut 列表, addDynamicShortcuts(List) 可以添加新的 shortcut 到列表,超过最大个数会报异常;

  • Update更新: updateShortcuts(List) 可以更新一组 Shortcuts;

  • Remove删除: removeDynamicShortcuts(List)removeAllDynamicShortcuts() 可以删除部分或所有shortcuts.

固定的 Shortcuts(Pinned Shortcuts)

指通过拖动固定到桌面的 Shortcuts,App 不可以添加、修改、删除这些 Shortcuts,只能禁用他们.即便 App 内删除了某个 Shorcut,对应的已固定到桌面的 Shortcuts 也不会被删除.

可以通过:

  1. getPinnedShortcuts() 得到所有固定的 Shortcuts 的信息.

  2. disableShortcuts(List)disableShortcuts(List, CharSequence) 禁用动态的 Shortcuts.

对于静态的 Shortcuts 需要在资源文件中设置 android:enabled="false" 进行禁用,不过没有必要,静态 Shortcuts 可直接通过删除达到禁用的效果,

静态 Shortcuts 和动态 Shortcuts 是有最大个数限制的,默认为 5,超过最大个数后添加会报异常.而固定的 Shortcuts 并没有个数限制,并且固定的 Shortcut 对应的 Shortcut 即便被动态删除了,依然可以通过 id 进行 Update 操作

动态 Shortcuts 操作的频率问题

当应该完全退到后台(无 Activity 或 Service 在前台时),其操作 Shortcut(包括添加、删除、修改) 的频率是受限的.可通过 isRateLimitingActive() 查询是否已受限,true表示已受限.

动态 Shortcuts 与静态 Shortcuts 区别

  1. 静态 Shortcuts 只能通过升级应用修改,动态 Shortcuts 随时可以修改;

  2. 静态 Shortcuts 的 Intent 无法设置 Flag,默认为 FLAG_ACTIVITY_NEW_TASK 和 FLAG_ACTIVITY_CLEAR_TASK Flag,即若应用运行中会清除所有已存在的 Activity.动态 Shortcuts 的 Intent 可以设置 Flag;

  3. 静态 Shortcuts 的rank系统默认根据声明顺序设置,动态 Shortcuts 的rank可以通过setRank(int rank)接口主动设置,rank 不能小于 0,值越大表示在 shortcut 列表展示时离 App Icon 越远.静态 Shortcuts 默认比动态 Shortcuts 离 App Icon 更近.

  4. 静态 Shortcuts 删除可以直接删除,动态 Shortcuts 建议通过禁用删除;

Shortcuts 最佳实践

  1. 不管是静态形式还是动态形式,每个应用最多可以注册4个Shortcuts.目前虽然说是 5 个,但实际最多只能添加 4 个(最多添加 4 个 Shortcuts 以保持在启动器中显示的样式最佳)
int count = shortcutManager.getMaxShortcutCountPerActivity()
  1. “short description” 限制在 10 个字符内

  2. “long description” 限制在 25 个字符内

  3. 改变 dynamic and pinned shortcuts时,调用方法 updateShortcuts()

  4. 重复创建shortcut时,动态的 shortcuts使用方法: addDynamicShortcuts()setDynamicShortcuts() Pinned shortcuts使用方法: requestPinShortcut().

  5. 在每次启动与需要重新发布动态shortcut时,推荐检查方法getDynamicShortcuts() 返回的数量.


文章作者: PudgeLee
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 PudgeLee !
评论
  目录