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 ...
随机推荐
- 生僻的mysql
1.show table status like 'user' 2.alter table mytableEngine=InnoDB 需要执行很长时间,mysql会按行将数据从原表复制到一张新的表中, ...
- MySQL 当前时间,今日时间,前日时间 详解
MySQL 获取当前日期及日期格式 获取系统日期: NOW() 格式化日期: DATE_FORMAT(date, format) 注: date:时间字段 format:日期格式 返回系统日期,输出 ...
- LeetCode 044 Wildcard Matching
题目要求:Wildcard Matching Implement wildcard pattern matching with support for '?' and '*'. '?' Matches ...
- LeetCode 037 Sudoku Solver
题目要求:Sudoku Solver Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells ...
- 20200311_最新爬取mzitu
废话不多, 直接上代码, python3.6: import requests from bs4 import BeautifulSoup import os import time; import ...
- moviepy AudioClip的max_volume方法报错ValueError: operands could not be broadcast together with shapes(2,)
☞ ░ 前往老猿Python博文目录 ░ 在<moviepy音视频剪辑:AudioClip的max_volume方法报TypeError: bad operand type for abs(): ...
- Python中自定义类未定义__lt__方法使用sort/sorted排序会怎么处理?
在<第8.23节 Python中使用sort/sorted排序与"富比较"方法的关系分析>中介绍了排序方法sort和函数sorted在没有提供key参数的情况下默认调用 ...
- jdk源码之 hashmap 与hashtable 的区别
hashmap hashtable 线程安全 否,但jdk5之后,提供ConcurrentHashMap,可 替代HashTable. 是,synchronized value是否允许为空 是 否 ...
- Java并发编程的艺术(二)——volatile、原子性
什么是volatile Java语言允许线程访问共享变量,为了确保共享变量能够被准确一致地更新,如果一个字段被声明为volatile,那么Java内存模型将会确保所有线程看到这个变量时值是一致的.保证 ...
- 使用docker与宿主机文件互相拷贝
1.从容器里面拷文件到宿主机 示例:容器名为s2-061_struts2_1,要从容器里面拷贝的文件路为:/usr/local/tomcat/webapps/test/js/test.js, 现在要将 ...