Android 7.1 App Shortcuts使用

Android 7.1 App Shortcuts使用

Android 7.1已经发了预览版, 这里是API Overview: API overview.
其中App Shortcuts是新提供的一种快捷访问方式, 形式为长按应用图标出现的长条.

app-shortcuts.png

图来自: Exploring Android Nougat 7.1 App Shortcuts

点击快捷方式可以访问应用功能, 并且这种快捷方式也可以被拖拽到桌面单独放置.

App Shortcuts 是什么

其中App Shortcuts是指在桌面长按app图标而出现的快捷方式, 可以为你的app的关键功能添加更快速的入口而不用先打开app.

app-shortcuts-doc.png

点击快捷方式可以访问应用功能, 并且这种快捷方式也可以被拖拽到桌面单独放置, 变成单独的桌面快捷方式(pinned shortcuts).

有两种shortcuts:

  • 静态的: 在xml中定义, 适用于一些通用的动作.
  • 动态的: 由ShortcutManager发布, 可以根据用户的行为或者偏好添加, 可以动态更新.

每一个应用目前最多可以有5个shortcuts(静态 + 动态).

运行条件:
应用添加App Shortcuts是Android 7.1(API 25)的API, 所以只能在Android 7.1的设备上显示, 同时需要launcher支持, 比如Pixel launcher(Pixel设备的默认launcher), Now launcher(Nexus设备上的launcher)现在就支持, 其他launcher也可以提供支持.

静态Shortcuts使用

静态的Shortcuts是写在xml中的, 直到下一次应用升级, 不能被改变.
要添加静态shortcuts只需两步:
首先, 在应用的Manifest中启动Activity上添加<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>

然后在res/xml/目录下创建shortcuts.xml文件, 里面包含静态的shortcuts:

<?xml version="1.0" encoding="utf-8"?>
<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
    <shortcut
        android:enabled="true"
        android:icon="@drawable/ic_check_circle_black_24dp"
        android:shortcutDisabledMessage="@string/static_shortcut_disabled_message"
        android:shortcutId="static"
        android:shortcutLongLabel="@string/static_shortcut_long_label_1"
        android:shortcutShortLabel="@string/static_shortcut_short_label_1">
        <intent
            android:action="android.intent.action.VIEW"
            android:targetClass="com.ddmeng.hellonougat.shortcuts.StaticShortcutActivity"
            android:targetPackage="com.ddmeng.hellonougat" />
    </shortcut>
    <shortcut
        android:enabled="true"
        android:icon="@drawable/ic_android_black_24dp"
        android:shortcutDisabledMessage="@string/static_shortcut_disabled_message"
        android:shortcutId="static_2"
        android:shortcutLongLabel="@string/static_shortcut_long_label_2"
        android:shortcutShortLabel="@string/static_shortcut_short_label_2">
        <intent
            android:action="android.intent.action.MAIN"
            android:targetClass="com.ddmeng.hellonougat.MainActivity"
            android:targetPackage="com.ddmeng.hellonougat" />
        <intent
            android:action="com.ddmeng.hellonougat.action.STATIC_SHORTCUT_2"
            android:targetClass="com.ddmeng.hellonougat.shortcuts.StaticShortcutActivity"
            android:targetPackage="com.ddmeng.hellonougat" />
    </shortcut>
</shortcuts>

这就好了, 这个文件添加了两个shortcuts, 点击都将打开指定的Activity, 本例子中是StaticShortcutActivity.

用多个Intent构建back stack

上面这个文件里添加了两个静态的shortcuts, 第一个关联了一个Activity, 点击shortcut将直接打开这个Activity, 回退的时候回到桌面.

如果你想要的效果是点击back键回到应用里的某个界面, 那么可以利用多个intents来构建back stack, 比如在第二个shortcut里面, 点击shortcut还是打开目标Activity, 这个指定目标Activity的Intent放在最后, 但是回退会返回到MainActivity, 即之前的那个Intent.

动态Shortcuts使用

动态的shortcuts可以在用户使用app的过程中构建, 更新, 或者删除.
使用ShortcutManager可以对动态shortcuts完成下面几种操作:

  • Publish发布: setDynamicShortcuts(), addDynamicShortcuts(List);
  • Update更新: updateShortcuts(List);
  • Remove删除: removeDynamicShortcuts(List), removeAllDynamicShortcuts().

比如添加一个动态shortcut:

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));

点击这个shortcut会发出一个打开网页的Intent, 让你选择浏览器, 从而打开网址.

多个Intent构建back stack

动态的shortcut仍然可以用多个Intent来指定一个back stack, 那么打开目标Activity之后就可以返回到应用中的指定界面而不是回到launcher:

ShortcutInfo dynamicShortcut2 = new ShortcutInfo.Builder(this, "shortcut_dynamic")
        .setShortLabel("Dynamic Shortcut")
        .setLongLabel("Open Dynamic shortcut 2")
        .setIcon(Icon.createWithResource(this, R.drawable.ic_favorite_border_black_24dp))
        .setIntents(
                // this dynamic shortcut set up a back stack using Intents, when pressing back, will go to MainActivity
                // the last Intent is what the shortcut really opened
                new Intent[]{
                        new Intent(Intent.ACTION_MAIN, Uri.EMPTY, this, MainActivity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK),
                        new Intent(DynamicShortcutActivity.ACTION_OPEN_DYNAMIC)
                        // intent's action must be set
                })
        .build();

和静态一样, 最后一个Intent对应的是shortcut打开的界面DynamicShortcutActivity, 前面的都是用来构建back stack, 即back退回到MainActivity.
注意这里的Intent必须指定Action, 否则会抛出异常.

Shortcuts的个数限制

Shortcuts的总数不能超过5个, 即静态和动态shortcuts加起来总数最多是五个.
当我们尝试添加第六个shortcut时, 应用会抛出异常: java.lang.IllegalArgumentException: Max number of dynamic shortcuts exceeded.

虽然总数限制是5个, 但是当我正好有5个(2个静态 + 3个动态)的时候, 长按只显示了4个shortcuts.
如图:

app-shortcuts-demo-screenshot.png

本文完整代码见: Demo地址: HelloNougat.

Shortcuts的次序

当我们有多个Shortcuts之后, 默认它们是按照添加顺序排列的, 即按照添加顺序rank递增.

可以通过setRank()来改变长按时它们显示的排序:

@TargetApi(25)
private void updateDynamicShortcuts() {
    ShortcutInfo webShortcut = new ShortcutInfo.Builder(MainActivity.this, "shortcut_blog")
            .setRank(1)
            .build();

    ShortcutInfo dynamicShortcut = new ShortcutInfo.Builder(MainActivity.this, "shortcut_dynamic")
            .setRank(0)
            .build();
    // the rank value can not be set to negative, otherwise will throw
    // java.lang.IllegalArgumentException: Rank cannot be negative or bigger than MAX_RANK

    // the static shortcuts have the rank 0, so they will always be closest to launcher icon

    shortcutManager.updateShortcuts(Arrays.asList(webShortcut, dynamicShortcut));
}

这样更改之后, 原先排在最远端的shortcut_dynamic被移到了第三个, shortcut_blog被移到了它的后面.

setRank()不接受负值, 会抛出异常.

我们只能改变动态shortcuts的排序, 静态的shortcuts等级为0, 它们是按照xml中写定的先后顺序排的, 所以:
静态的shortcuts永远离应用icon最近, 动态shortcuts在其之上排序, rank越大的离应用icon越远.
如果没有指定rank, 则按生成的顺序递增.

参考

App Shortcuts的官方文档: App Shortcuts
Exploring Android Nougat 7.1 App Shortcuts

Demo地址: HelloNougat.
近期考虑加入更多Android 7 Nougat特性sample.

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

推荐阅读更多精彩内容