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

Hello shortcuts

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

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>
  1. 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标签内的<intent/>标签可以有多个,例如页面1、页面2、页面3,用户点击shortcuts后进入的是列表的最后一个(即页面3),并且可以依次回退到页面2、页面1.

动态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不会显示张三、李四,但桌面上的张三李四不会消失且可以正常使用。

  1. 使用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();
    ...
    
  2. 使用removeDynamicShortcuts()移除单个或多个动态shortcuts,使用removeAllDynamicShortcuts()移除所有的动态shortcuts

    shortcutManager.removeDynamicShortcuts(Arrays.asList("shortCutId31", "shortCutId4"));
    
    shortcutManager.removeAllDynamicShortcuts();
    
  3. 设置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

追踪shortcuts的使用

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

  1. 用户点击了shortcuts

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

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

shortcuts设计规范

App Shortcuts Design Guidelines

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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,736评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,167评论 1 291
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,442评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,902评论 0 204
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,302评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,573评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,847评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,562评论 0 197
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,260评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,531评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,021评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,367评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,016评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,068评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,827评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,610评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,514评论 2 269

推荐阅读更多精彩内容