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 ...
随机推荐
- Jmeter-BeanShell断言的运用一(JSON响应数据与数据库比对)
前言 最近在学习BeanShell断言,发现有点强大哈,只要会写代码,就没有什么是断言不了的,哈哈哈,不过我现在只会写点蹩脚的代码,下面将介绍下如何将返回的JSON数据与数据库数据做对比. 注:本次涉 ...
- C#中的WinForm问题——如何设置窗体的大小为超过屏幕显示的最大尺寸?
今天在学习C#时遇到了一个问题,在此记录下来,留作日后总结复习之用,也分享给有同样问题和困扰的园友. 我手上的电脑是笔记本电脑,屏幕的尺寸大小为1366*768,然而项目所使用的屏幕大小为1920*1 ...
- 冰河开源了全网首个完全开源的分布式全局有序序列号(分布式ID)框架!!
写在前面 mykit-serial框架的设计参考了李艳鹏大佬开源的vesta框架,并彻底重构了vesta框架,借鉴了雪花算法(SnowFlake)的思想,并在此基础上进行了全面升级和优化.支持嵌入式( ...
- 转:csdn怎么快速别人的文章
在csdn看到好的文章想转载,无奈找不到转载的功能,只能想办法了. 首先确定原文允许转载 在文章开头处一般有版权声明,如图 转载时要注明出处和作者 如何转载 用谷歌浏览器加载文章地址,打开文章 F12 ...
- 第11.16节 Python正则元字符“()”(小括号)与组(group)匹配模式
一. 什么是组 关于组匹配模式,Python官网上说得比较简单,也没有这个名词,只有组这个名词,老猿查了比较多的资料和做了相关测试之后才理解. 组匹配模式,就是在匹配的正则表达式中使用小括号" ...
- PyQt(Python+Qt)学习随笔:Qt Designer中主窗口对象documentMode属性
documentMode属性表示当前主窗口是否启用文档模式,如果是则主窗口的选项卡部件会以适合操作文档的模式呈现,这类似于macOS上的文档模式. 设置此属性时,界面上不会呈现选项卡部件框架.此模式当 ...
- IAR环境定义位变量标志位 STM8 MSP430通用
首先建立一个公共点H文件,加入通用代码如下 typedef union { struct { unsigned char b0:1; unsigned char b1:1; unsigned char ...
- 题解 CF504E 【Misha and LCP on Tree】
PullShit 倍增和树剖的差距!!! 一个 TLE, 一个 luogu 最优解第三!!! 放个对比图(上面倍增,下面轻重链剖分): 不过这是两只 log 非正解... Solution \(LCP ...
- Acwing 734. 能量石
贪心(微扰) + dp 这道题还是比较难的,前置知识: 贪心的微扰(邻项交换)证法,例题:国王游戏,耍杂技的牛 01背包 算法1:暴力\(O(T * n! * n)\) 可以\(dfs\)全排列枚举所 ...
- 谈谈MySQL bin log的写入机制、以及线上的参数是如何配置的
目录 一.binlog 的高速缓存 二.刷盘机制 三.推荐的策略 推荐阅读 问个问题吧!为什么你需要了解binlog的落盘机制呢? 我来回答一下: 上一篇文章提到了生产环境中你可以使用binlog ...