Android 7.1已经发了预览版, 这里是API Overview: API overview.
其中App Shortcuts是新提供的一种快捷访问方式, 形式为长按应用图标出现的长条.html
图来自: Exploring Android Nougat 7.1 App Shortcutsjava
点击快捷方式能够访问应用功能, 而且这种快捷方式也能够被拖拽到桌面单独放置.android
其中App Shortcuts是指在桌面长按app图标而出现的快捷方式, 能够为你的app的关键功能添加更快速的入口而不用先打开app.git
点击快捷方式能够访问应用功能, 而且这种快捷方式也能够被拖拽到桌面单独放置, 变成单独的桌面快捷方式(pinned shortcuts).github
有两种shortcuts:web
每个应用目前最多能够有5个shortcuts(静态 + 动态).api
运行条件:
应用添加App Shortcuts是Android 7.1(API 25)的API, 因此只能在Android 7.1的设备上显示, 同时须要launcher支持, 好比Pixel launcher(Pixel设备的默认launcher), Now launcher(Nexus设备上的launcher)如今就支持, 其余launcher也能够提供支持.浏览器
静态的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:app
<?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
.
上面这个文件里添加了两个静态的shortcuts, 第一个关联了一个Activity, 点击shortcut将直接打开这个Activity, 回退的时候回到桌面.
若是你想要的效果是点击back键回到应用里的某个界面, 那么能够利用多个intents来构建back stack, 好比在第二个shortcut里面, 点击shortcut仍是打开目标Activity, 这个指定目标Activity的Intent放在最后, 可是回退会返回到MainActivity, 即以前的那个Intent.
动态的shortcuts能够在用户使用app的过程当中构建, 更新, 或者删除.
使用ShortcutManager能够对动态shortcuts完成下面几种操做:
setDynamicShortcuts()
, addDynamicShortcuts(List)
;updateShortcuts(List)
;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, 让你选择浏览器, 从而打开网址.
动态的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的总数不能超过5个, 即静态和动态shortcuts加起来总数最可能是五个.
当咱们尝试添加第六个shortcut时, 应用会抛出异常: java.lang.IllegalArgumentException: Max number of dynamic shortcuts exceeded
.
虽然总数限制是5个, 可是当我正好有5个(2个静态 + 3个动态)的时候, 长按只显示了4个shortcuts.
如图:
本文完整代码见: Demo地址: HelloNougat.
当咱们有多个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.
最后, 欢迎关注微信公众号: 圣骑士Wind