App Shortcuts 快捷方式:Android 的 '3D Touch'
Hello Shortcuts
从Android7.1(API level25)开始,开发者可以为自己的app定制shortcuts。shortcuts使用户更便捷、快速的使用app。我个人感觉有点像ios的压力感应,但是我认为Google的shortcuts动画做的更好看:)。
shortcuts分为两种:
Static shortcuts:静态shortcuts是在资源文件中定义的,所以你只能通过升级你的app来更新静态shortcuts的相关信息。
Dynamic shortcuts:动态shortcuts是通过ShortcutManager相关的API来实现运行时新增、修改、移除shortcuts的。
另外关于shortcuts有以下几点小tips:
最多可以设置5个快捷方式,(但经测试最多只能显示4个)。有些启动器(launcher app)不会显示出你添加的所有的快捷方式。
用户可以长按shortcuts将其固定到桌面,Google称其为“pinned shortcuts”,pinned shortcuts的数量是没有限制的,并且开发者无权移除这些pinned shorcuts(只能用户自己移除或者删除app后自动移除,如果某个shortcuts已经被固定到桌面,即使动态删除了该shortcuts,桌面的shortcuts也不会消失且可以正常使用),但可以将其设为不可用状态(disbale)。
虽然其他app无法通过shortcuts访问你的app的元数据(metadata),但启动器(laucher)可以,所以在使用shortcuts时要注意保护用户的隐私信息。
静态shortcuts(Static Shortcuts)的使用
- 清单文件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>
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(Dynamic Shortcuts)的使用
使用ShortcutManager相关API来创建、更新、移除shortcuts。几种API的使用方法都类似。
- 使用 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不会显示张三、李四,但桌面上的张三李四不会消失且可以正常使用。
使用 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();
...
使用 removeDynamicShortcuts() 移除单个或多个动态shortcuts,使用 removeAllDynamicShortcuts() 移除所有的动态shortcuts
shortcutManager.removeDynamicShortcuts(Arrays.asList("shortCutId31", "shortCutId4")); shortcutManager.removeAllDynamicShortcuts();
设置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()
:
用户点击了shortcuts
用户操作了与shortcuts关联的操作
上报shortcuts的使用来预测shortcuts的优先级,帮助开发者更好得使用shortcuts。
shortcuts设计规范
App Shortcuts Design Guidelines
Android从material design设计规范推出开始到今天的Android7,个人觉得真的是不输iOS,然而至今没有广泛地被推崇,实在令人不免一声叹息。另外手上2013年发布的Nexus5升级Android7之后竟然感觉比以前还顺滑,什么叫良心?跟我一起大喊:Google大法好!
App Shortcuts 快捷方式:Android 的 '3D Touch'的更多相关文章
- Android 7.1 App Shortcuts使用
Android 7.1 App Shortcuts使用 Android 7.1已经发了预览版, 这里是API Overview: API overview. 其中App Shortcuts是新提供的一 ...
- Android 7.1 - App Shortcuts
Android 7.1 - App Shortcuts 版权声明:本文为博主原创文章,未经博主允许不得转载. 微博:厉圣杰 源码:AndroidDemo/Shortcuts 文中如有纰漏,欢迎大家留言 ...
- Android 7.1.1 之实现 3D Touch
转载请注明出处:http://blog.csdn.net/yyh352091626/article/details/68962736 Shortcut概念 详细实现 BuildConfig 配置 静态 ...
- 3D Touch介绍:电子秤App与快捷操作
随着iPhone6s与6s plus的到来,苹果给我们展现了一种全新的交互方式:重按手势.你可能知道,这个特性已经在Apple Watch和MacBook上推出了,不过那时叫Force Touch,就 ...
- 微信成为首批支持iPhone 6s /Plus 上 3D Touch 功能的 App
2015苹果新品发布会上微信成为首批支持iPhone 6s 和 iPhone 6s Plus 上 3D Touch 功能的 App.通过 3D Touch,微信用户将可以通过更精减的操作完成基本任务, ...
- Android中为APP创建快捷方式的原理(自己的理解)
我们首先来看Android中为APP创建快捷方式的原理: 从图上可以看出,Android大致分7步完成快捷方式的创建: 第一步:Android系统的launcher程序会调用它的pickShortcu ...
- iOS开发 - 3D Touch 应用系列一 - Quick Actions 创建桌面 Icon 快捷方式
个言 很久没发随笔了,有一年多了吧.期间也曾想继续去写随笔,但是因为各种原因而耽搁了.最近又想了一下,还是有很多东西想要写,想要分享,想要记录下来的东西.之后我也会不断写随笔,但不止于 iOS 的方向 ...
- 从3D Touch 看 原生快速开发
全新的按压方式苹果继续为我们带来革命性的交互:Peek和Pop,Peek 和 Pop 让你能够预览所有类型的内容,甚至可对内容进行操作,却不必真的打开它们.例如,轻按屏幕,可用 Peek 预览收件箱中 ...
- 3D touch 的 应用 --备用
在iPhone 6s和iPhone 6s Plus中Apple引入了3D Touch技术.3D Touch的触控技术,被苹果称为新一代多点触控技术.其实,就是此前在Apple Watch上采用的For ...
随机推荐
- oracle 流程控制句式
--for loop declare val number(10):=0; begin for val in 0..10 loop dbms_output.put_line('val='||val); ...
- Integer中的奇妙位运算
Integer中的奇妙位运算 参考资料 https://segmentfault.com/a/1190000015763941 highestOneBit(int i) 函数的作用是获得传入参数的最高 ...
- Spring Cloud 学习 (八) Spring Boot Admin
Spring Boot Admin 用于管理和监控一个或者多个 Spring Boot 程序 新建 spring-boot-admin-server pom <parent> <ar ...
- 基于CefSharp开发(二)自定义浏览器窗体
上一篇 https://www.cnblogs.com/mchao/p/13914726.html 简单了解了CefSharp引用配置但页面光秃秃的,这一篇着手开发简单浏览器窗体 一.Edge浏览器窗 ...
- 第15.26节 PyQt(Python+Qt)入门学习:Model/View架构中的便利类QListWidget详解
老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 一.概述 列表部件(List Widget)对应类QListWidget,是从QListView派生 ...
- PyQt(Python+Qt)学习随笔:toolButton的toolButtonStyle属性
toolButtonStyle属性用于确认toolButton按钮显示文字.图标的方式,其类型为枚举类型 Qt.ToolButtonStyle,有如下值: ToolButtonIconOnly(值为0 ...
- 本地web项目部署到服务器里连接不上数据库的解决办法
今天突然想到把自己之前的项目挂到服务器上,但是用到了数据库,于是给服务器装上了MySQL,想着能赶紧把项目挂上去看看效果,然后并不是一帆风顺,在奋斗了四小时后终于解决了问题的所在. (1)首先我找到了 ...
- 推荐系统实践 0x0c FM系列
逻辑回归(LR) 在介绍FM系列之前,我想首先简单介绍一下逻辑回归.通常来说,逻辑回归模型能够综合利用更多的信息,如用户.物品.上下文等多种不同的特征,生成更为全面的结果.另外,逻辑回归将推荐问题看成 ...
- IntelliJ IDEA2019.3.2破解/永久激活/安装教程
我想大家用过史上最好的开发工具就是idea了,没有之一!看到大家都在找idea的激活教程,今天我也在这里跟大家分享一下. 本教程针对现在官网针对的版本是idea2019.3.2,为防止以后会更新破解失 ...
- Java对象操作工具
对象复制(反射法) public static void copyProp(Object from, Object to, String... filterProp) { HashSet<Str ...