App Shortcuts 快捷方式 Android7.1 的"3D Touch"

Hello shortcuts

从Android7.1(API level25)开始,开发者可以为自己的app定制shortcuts。shortcuts使用户更便捷、快速的使用app。我个人感觉有点像ios的压力感应,但是我认为Google的shortcuts动画做的更好看:)。

shortcuts分为两种:

  1. Static shortcuts:静态shortcuts是在资源文件中定义的,所以你只能通过升级你的app来更新静态shortcuts的相关信息。

  2. Dynamic shortcuts:动态shortcuts是通过ShortcutManager相关的API来实现运行时新增、修改、移除shortcuts的。

另外关于shortcuts有以下几点小tips:

  1. 最多可以设置5个快捷方式,(但经测试最多只能显示4个)。有些启动器(launcher app)不会显示出你添加的所有的快捷方式。

  2. 用户可以长按shortcuts将其固定到桌面,Google称其为“pinned shortcuts”,pinned shortcuts的数量是没有限制的,并且开发者无权移除这些pinned shorcuts(只能用户自己移除或者删除app后自动移除,如果某个shortcuts已经被固定到桌面,即使动态删除了该shortcuts,桌面的shortcuts也不会消失且可以正常使用),但可以将其设为不可用状态(disbale)。

  3. 虽然其他app无法通过shortcuts访问你的app的元数据(metadata),但启动器(laucher)可以,所以在使用shortcuts时要注意保护用户的隐私信息。

这里写图片描述

静态shortcuts(Static Shortcuts)的使用

  1. 清单文件Manifest中在启动页添加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>
  2. res文件夹内新建文件夹xml,新建文件shortcuts.xml

    //shortcuts.xml
    
    <shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
    <shortcut
        android:shortcutId="shortCutId1"
        android:enabled="true"
        android:icon="@drawable/icon_android"
        android:shortcutShortLabel="@string/compose_shortcut_short_label1"
        android:shortcutLongLabel="@string/compose_shortcut_long_label1"
        android:shortcutDisabledMessage="@string/compose_disabled_message1">
        <intent
            android:action="android.intent.action.VIEW"
            android:targetPackage="com.example.zengyazhi.myapplication"
            android:targetClass="com.example.zengyazhi.myapplication.Main1Activity" />
        <intent
            android:action="android.intent.action.VIEW"
            android:targetPackage="com.example.zengyazhi.myapplication"
            android:targetClass="com.example.zengyazhi.myapplication.Main2Activity" />
        <categories android:name="android.shortcut.conversation" />
    </shortcut>
    
    <shortcut
        android:shortcutId="shortCutId2"
        android:enabled="true"
        android:icon="@drawable/icon_google"
        android:shortcutShortLabel="@string/compose_shortcut_short_label2"
        android:shortcutLongLabel="@string/compose_shortcut_long_label2"
        android:shortcutDisabledMessage="@string/compose_disabled_message2">
        <intent
            android:action="android.intent.action.VIEW"
            android:targetPackage="com.example.zengyazhi.myapplication"
            android:targetClass="com.example.zengyazhi.myapplication.Main2Activity" />
        <categories android:name="android.shortcut.conversation" />
    </shortcut>

    解释一下shortcut标签里的各个的属性:

    • shortcutId:shortcuts的ID
    • enabled:如果为false则不会在长按shortcuts列表中显示
    • icon:shortcuts的图标
    • shortcutShortLabel:当shortcuts固定到桌面时的标题(注意字符串只能使用string资源文件引用,不可以直接使用字符串)
    • shortcutLongLabel:长按app出现shortcuts时的标题,如果太长或未设置默认会显示shortcutShortLabel
    • shortcutDisabledMessage: 当pinned shortcuts不可用时的toast提示信息
    • shortcuts标签内的标签可以有多个,例如页面1、页面2、页面3,用户点击shortcuts后进入的是列表的最后一个(即页面3),并且可以依次回退到页面2、页面1.

//静态shortcuts gif

动态shortcuts(Dynamic Shortcuts)的使用

使用ShortcutManager相关API来创建、更新、移除shortcuts。几种API的使用方法都类似。

  1. 使用setDynamicShortcuts()addDynamicShortcuts()来动态增加shortcuts。这两个方法有点相似,使用时要注意区别。
    addDynamicShortcuts():添加shortcuts,如果存在相同的ID的shortcuts则更新信息。
    setDynamicShortcuts()替换掉已有的动态shortcuts列表,如果存在相同的ID的shortcuts则更新信息。举个例子:
    例如原本有shortcuts列表:

    • 标签一(lable:张三, id:one)
    • 标签二(lable:李四, id:two)
    • 标签三(lable:王五, id:three)

    使用setDynamicShortcuts()方法并传入两个ShortcutInfo:

    • 标签三(lable:赵六, id:three)
    • 标签四(lable:钱七, id:four)

    调用方法后会使shortcuts列表则变为:

    • 标签三(lable:赵六, id:three)
    • 标签四(lable:钱七, id:four)

    因为标签三的ID相同,所以更新标签三,而shortcuts列表中原来的标签一、二被移除,而且一开篇介绍shortcuts时也提到过是:如果shortcuts已被固定到桌面成为pinned shortcuts,即使shortcuts从列表中被移除了,但桌面的pinned shortcuts依然可以正常使用,除非你将其设置disable状态,即如果张三、李四被固定到桌面,即使调用setDynamicShortcuts()后长按app不会显示张三、李四,但桌面上的张三李四不会消失且可以正常使用。

  2. 使用updateShortcuts()更新shortcuts的信息

    ShortcutManager shortcutManager = getSystemService(ShortcutManager.class);
    ShortcutInfo shortCutId1 = new ShortcutInfo.Builder(MainActivity.this, "shortCutId3")
            .setShortLabel("更改桌面标签3")
            .setLongLabel("更改快捷方式标签3")
            .setIcon(Icon.createWithResource(this, R.drawable.icon_chrome))
            .setDisabledMessage("更改不可用时提示信息3")
            .setIntent(new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.baidu.com/"), this, Main1Activity.class))
            .build();
    shortcutManager.updateShortcuts(Arrays.asList(shortCutId1, shortCutId4));

    动态shortcuts也可以像静态shortcuts一样同时添加多个intent意图

    Intent intent1 = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.baidu.com/"), this, Main1Activity.class);
    Intent intent2 = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.baidu.com/"), this, Main2Activity.class);
    Intent[] intents = {intent1, intent2};
    
    ...
    .setIntents(intents)
    .build();
    ...
  3. 使用removeDynamicShortcuts()移除单个或多个动态shortcuts,使用removeAllDynamicShortcuts()移除所有的动态shortcuts

    shortcutManager.removeDynamicShortcuts(Arrays.asList("shortCutId31", "shortCutId4"));
    
    shortcutManager.removeAllDynamicShortcuts();
  4. 设置shortcuts为不可用状态:

    shortcutManager.disableShortcuts(Arrays.asList("shortCutId3"));
    //另一个重载的方法,可以在用户点击该shortcuts时显示错误信息
    shortcutManager.disableShortcuts(Arrays.asList("shortCutId4"), "快捷方式4已不可用");

需要注意的是:以上API只能操作动态shortcuts(包括pinned shortcuts),不可操作静态shortcuts,如果传入静态shortcuts的id会报IllegalArgumentException错误:Manifest shortcut ID=*** may not be manipulated via APIs

追踪shortcuts的使用

官网文档中提到以下两种情景需要调用reportShortcutUsed():

  1. 用户点击了shortcuts

  2. 用户操作了与shortcuts关联的操作

上报shortcuts的使用来预测shortcuts的优先级,帮助开发者更好得使用shortcuts。

shortcuts设计规范

App Shortcuts Design Guidelines

Android从material design设计规范推出开始到今天的Android7,个人觉得真的是不输iOS,然而至今没有广泛地被推崇,实在令人不免一声叹息。另外手上2013年发布的Nexus5升级Android7之后竟然感觉比以前还顺滑,什么叫良心?跟我一起大喊:Google大法好!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值