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

图来自: Exploring Android Nougat 7.1 App Shortcuts
点击快捷方式可以访问应用功能, 并且这种快捷方式也可以被拖拽到桌面单独放置.
App Shortcuts 是什么
其中App Shortcuts是指在桌面长按app图标而出现的快捷方式, 可以为你的app的关键功能添加更快速的入口而不用先打开app.

点击快捷方式可以访问应用功能, 并且这种快捷方式也可以被拖拽到桌面单独放置, 变成单独的桌面快捷方式(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.
如图:

本文完整代码见: 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.
最后, 欢迎关注微信公众号: 圣骑士Wind

Android 7.1 App Shortcuts使用的更多相关文章
- Android 7.1 - App Shortcuts
Android 7.1 - App Shortcuts 版权声明:本文为博主原创文章,未经博主允许不得转载. 微博:厉圣杰 源码:AndroidDemo/Shortcuts 文中如有纰漏,欢迎大家留言 ...
- App Shortcuts 快捷方式:Android 的 '3D Touch'
Hello Shortcuts 从Android7.1(API level25)开始,开发者可以为自己的app定制shortcuts.shortcuts使用户更便捷.快速的使用app.我个人感觉有点像 ...
- Android 7.1 快捷方式 Shortcuts
转载请注明出处:王亟亟的大牛之路 前些天就看到相关内容了,但是最近吸毒比较深(wow),所以没有紧跟潮流,今天补一篇. 先安利:https://github.com/ddwhan0123/Useful ...
- Android中实现APP文本内容的分享发送与接收方法简述
谨记(指定选择器Intent.createChooser()) 开始今天的内容前,先闲聊一下: (1)突然有一天头脑风暴,对很多问题有了新的看法和见解,迫不及待的想要分享给大家,文档已经写好了,我需要 ...
- 在布局中使用android.support.v4.app.Fragment的注意事项
1.Activity必须继承android.support.v4.app.FragmentActivity 2.fragment标签的name属性必须是完全限定包名,如下: <LinearLay ...
- Android 中如何计算 App 的启动时间?
(转载) 已知的两种方法貌似可以获取,但是感觉结果不准确:一种是,adb shell am start -w packagename/activity,这个可以得到两个值,ThisTime和Total ...
- Android版-支付宝APP支付
此项目已开源 赶快来围观 Start支持下吧 [客户端开源地址-JPay][服务端端开源地址-在com.javen.alipay 包名下] 上一篇详细介绍了微信APP支付 点击这里 此篇文章来详细介绍 ...
- app:transformClassesWithJarMergingForDebug uplicate entry: android/support/v4/app/BackStackState$1.class
.Execution failed for task ':app:transformClassesWithJarMergingForDebug'.> com.android.build.api. ...
- android.support.v4.app.Fragment和android.app.Fragment区别
1.最低支持版本不同 android.app.Fragment 兼容的最低版本是android:minSdkVersion="11" 即3.0版 android.support.v ...
随机推荐
- 您真的理解了SQLSERVER的日志链了吗?
您真的理解了SQLSERVER的日志链了吗? 先感谢宋沄剑给本人指点迷津,还有郭忠辉童鞋今天在QQ群里抛出的问题 这个问题跟宋沄剑讨论了三天,再次感谢宋沄剑 一直以来,SQLSERVER提供了一个非常 ...
- mono ios莫名其妙闪退的解决方法
使用mono进行ios开发也有一年了,一直有个头疼的问题是闪退,而且闪退的时候并没有抛出明确的错误. 前两天在调试一个bug的时候,在序列化的时候又莫名其妙的闪退,后来在一位大神(博客地址)的指导下, ...
- 【完全开源】知乎日报UWP版(上篇):界面设计、官方API分析
目录 说明 使用Fiddler分析android版API 部分效果图 关于源码 说明 在做博客园UWP版的时候其实就有做知乎日报的打算了,前段时间一直出差,在酒店里用Fiddler简单的分析了一下An ...
- 用SSH访问内网主机的方法
如今的互联网公司通常不会直接自己直接配主机搭建服务器了,而是采用了类似阿里云的这种云主机,当应用变得越来越大了之后,就不可避免地增加主机,而出于成本考虑,不可能给每一台主机都分配公网带宽,所以实际的情 ...
- ASP.NET Web API Model-ModelMetadata
ASP.NET Web API Model-ModelMetadata 前言 前面的几个篇幅主要围绕控制器的执行过程,奈何执行过程中包含的知识点太庞大了,只能一部分一部分的去讲解,在上两篇中我们看到在 ...
- Restful WebApi项目开发实践
前言 踩过了一段时间的坑,现总结一下,与大家分享,愿与大家一起讨论. Restful WebApi特点 WebApi相较于Asp.Net MVC/WebForm开发的特点就是前后端完全分离,后端使用W ...
- CentOS 安装OciLib 4.2.1 (Linux)
项目要用oracle , Windows的 OciLib 好弄, 今天安装到linux下 ,编译老是出错,最后几行如下: checking for OCILIB install path... /us ...
- Jquery
使用时jquery先引进jquery文件包 <script src="jquery-1.11.2.min.js"></script> 一个页面有多个文件jq ...
- js倒计时-倒计输入的时间
计算指定时间到指定时间之间相差多少天.时.分.秒. 节日.活动.商城常用. 原理: 主要使用到时间戳,也就是从1970 年 1 月 1 日 到指定时间的毫秒数. 1. 求出毫秒差 :当两个时间直接进行 ...
- VS 2015相当不错的功能:C#交互窗口
按照惯例,老周是先吹牛后讲正事.今天就给大伙吹吹这个事. 有网友不知道是不是昨晚喝高了,居然研究起老周来了.实话告诉你,老周没什么好研究的,老周又不是编译器,老周只是一个游离于大善大恶之间的平凡人,说 ...