最近在使用Android版Chrome的时候发现长按应用图标会出现如下图所示的快捷键:

这种新特性被我发现之后,我又找了几个出名的应用尝试了一下,又发现支付宝、UC浏览器这些APP也接入了这一新特性。接着我又去找了度娘,从度娘的搜索结果中我才知道这一新特性在Android7.0的时候就被提出来了,只是在国内的APP中应用的不广泛罢了。经过阅读网上大牛的技术博客以及自己的尝试,我自己对此新特性有了一定的认识。这个新特性的使用方式有点类似于广播,一个是静态使用,一个是动态使用。

一、静态使用shortcuts

1.1 创建资源文件

在res目录下创建xml文件夹,创建成功之后继续在该文件夹下shortcuts的资源文件。Shortcuts.xml的源代码如下所示:

<?xml version="1.0" encoding="utf-8"?>
<!--静态方式的使用,产生的标签最多也只有4个-->
<shortcuts xmlns:android="http://schemas.android.com/apk/res/android"><!--android:enabled用来区分当前快捷方式是否激活,值为false表示当前条目失效,值为true表示当前条目激活;失效的时候依次类推android:icon用来指定条目的图标android:shortcutId用来指定条目的唯一识别名android:shortcutLongLabel用来指定条目的长标签android:shortcutShortLabel用来指定条目短标签android:shortcutDisabledMessage用来指定条目未被激活时条目被点击时候的弹出信息--><shortcut
         android:enabled="true"android:icon="@drawable/ic_first"android:shortcutDisabledMessage="@string/first_disable_message"android:shortcutId="first"android:shortcutLongLabel="@string/first_long_label"android:shortcutShortLabel="@string/first_short_label"><!--android:action指定开启Activity的意图android:targetClass指定条目的目标类名android:targetPackage必须是当前应用的包名,否则会提示“未安装该应用”--><intent
             android:action="android.intent.action.VIEW"android:targetClass="com.study.yang.staticshortcutsdemo.static_short.FirstStaticActivity"android:targetPackage="com.study.yang.staticshortcutsdemo" /><!--此处可填可不填,默认即可--><categories android:name="android.shortcut.conversation"/></shortcut><shortcut
         android:enabled="true"android:icon="@drawable/ic_second"android:shortcutDisabledMessage="@string/second_disable_message"android:shortcutId="second"android:shortcutLongLabel="@string/second_long_label"android:shortcutShortLabel="@string/second_short_label"><!--类似栈算法由上到下--><intent
             android:action="android.intent.action.VIEW"android:targetClass="com.study.yang.staticshortcutsdemo.static_short.SecondStaticActivity"android:targetPackage="com.study.yang.staticshortcutsdemo" /><!--点击此条目时首先显示的是MainActivity,其次才是SecondStaticActivity--><intent
             android:action="android.intent.action.VIEW"android:targetClass="com.study.yang.staticshortcutsdemo.MainActivity"android:targetPackage="com.study.yang.staticshortcutsdemo" /></shortcut><shortcut
         android:enabled="true"android:icon="@drawable/ic_third"android:shortcutDisabledMessage="@string/third_disable_message"android:shortcutId="third"android:shortcutLongLabel="@string/third_long_label"android:shortcutShortLabel="@string/third_short_label"><intent
             android:action="android.intent.action.VIEW"android:targetClass="com.study.yang.staticshortcutsdemo.static_short.ThirdStaticActivity"android:targetPackage="com.study.yang.staticshortcutsdemo" /></shortcut><shortcut
         android:enabled="true"android:icon="@drawable/ic_forth"android:shortcutDisabledMessage="@string/forth_disable_message"android:shortcutId="forth"android:shortcutLongLabel="@string/forth_long_label"android:shortcutShortLabel="@string/forth_short_label"><intent
             android:action="android.intent.action.VIEW"android:targetClass="com.study.yang.staticshortcutsdemo.static_short.ForthStaticActivity"android:targetPackage="com.study.yang.staticshortcutsdemo" /></shortcut><!--从此处开始以下的shortcut都不会显示--><shortcut
         android:enabled="true"android:icon="@drawable/ic_fifth"android:shortcutDisabledMessage="@string/fifth_disable_message"android:shortcutId="fifth"android:shortcutLongLabel="@string/fifth_long_label"android:shortcutShortLabel="@string/fifth_short_label"><intent
             android:action="android.intent.action.VIEW"android:targetClass="com.study.yang.staticshortcutsdemo.static_short.FifthStaticActivity"android:targetPackage="com.study.yang.staticshortcutsdemo" /></shortcut><shortcut
         android:enabled="true"android:icon="@drawable/ic_sixth"android:shortcutDisabledMessage="@string/sixth_disable_message"android:shortcutId="sixth"android:shortcutLongLabel="@string/sixth_long_label"android:shortcutShortLabel="@string/sixth_short_label"><intent
             android:action="android.intent.action.VIEW"android:targetClass="com.study.yang.staticshortcutsdemo.static_short.SixthStaticActivity"android:targetPackage="com.study.yang.staticshortcutsdemo" /></shortcut></shortcuts>

1.2 在清单文件中使用shortcuts.xml文件

Shortcuts.xml文件必须在带有如下标签的内容的activity标签中使用,具体使用如下所示:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"package="com.study.yang.staticshortcutsdemo"><application
        android:allowBackup="true"android:icon="@mipmap/ic_launcher"android:label="@string/app_name"android:roundIcon="@mipmap/ic_launcher_round"android:supportsRtl="true"android:theme="@style/AppTheme"><activity android:name=".MainActivity"><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter><!--静态注册shortcuts.xml文件必须注册在应用的启动Activity下面--><meta-data
                android:name="android.app.shortcuts"android:resource="@xml/shortcuts"/></activity><activity android:name=".static_short.FirstStaticActivity" /><activity android:name=".static_short.SecondStaticActivity" /><activity android:name=".static_short.ThirdStaticActivity" /><activity android:name=".static_short.ForthStaticActivity" /><activity android:name=".static_short.FifthStaticActivity" /><activity android:name=".static_short.SixthStaticActivity"></activity></application></manifest>

最终效果图如下所示:

二、动态使用shortcuts

在正式的使用中我们还是用动态比较好,因为其可配置。动态使用shortcuts的核心类就是ShortcutManager,由其进行shortcuts的添加、修改、删除的操作。

2.1 添加shortcuts

此处的代码实现如下所示:

/*** 动态添加Shortcuts应用*/private void addDynamicShortcuts() {if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {mShortcutManager = (ShortcutManager) getSystemService(SHORTCUT_SERVICE);//返回至是4int maxShortcutCountPerActivity = mShortcutManager.getMaxShortcutCountPerActivity();List<ShortcutInfo> infos = new ArrayList<>();for (int i = 0; i < maxShortcutCountPerActivity; i++) {Intent intent = new Intent(this, MessageActivity.class);intent.setAction(Intent.ACTION_VIEW);intent.putExtra("message", "我和好汉" + mAdapter.getItem(i) + "的切磋");ShortcutInfo info = new ShortcutInfo//添加shortcuts条目的唯一id.Builder(this, "id" + i)//给条目添加短标签.setShortLabel(mAdapter.getItem(i))//给条目添加长标签.setLongLabel("水浒好汉:" + mAdapter.getItem(i))//给条目设置图标.setIcon(Icon.createWithResource(this, R.drawable.ic_first))//设置点击条目之后要触发的intent.setIntent(intent).build();infos.add(info);}mShortcutManager.addDynamicShortcuts(infos);}}

效果图如下所示:

2.2 修改shortcuts

此处的代码实现如下所示:

//给ListView添加单击事件进行修改shortcutlv.setOnItemClickListener(new AdapterView.OnItemClickListener() {@Overridepublic void onItemClick(AdapterView<?> parent, View view, int position, long id) {data.add(0, "刘备、张飞、关羽");mAdapter.notifyDataSetChanged();updateShortcut(position);}});/*** 更新Shortcut* @param index*/@TargetApi(Build.VERSION_CODES.N_MR1)private void updateShortcut(int index) {Intent intent = new Intent(this, MessageActivity.class);intent.setAction(Intent.ACTION_VIEW);intent.putExtra("message", "我和好汉" + mAdapter.getItem(index) + "的切磋");ShortcutInfo info = new ShortcutInfo//给指定的id重新赋值内容.Builder(this, "id" + index).setShortLabel(mAdapter.getItem(index)).setLongLabel("水浒好汉:" + mAdapter.getItem(index)).setIcon(Icon.createWithResource(this, R.drawable.ic_first))//此处可以添加多个intent.setIntents(new Intent[]{intent, new Intent("android.intent.action.MAIN", null, this, MainActivity.class)}).setDisabledMessage("好汉失效了").build();//更新mShortcutManager.updateShortcuts(Arrays.asList(info));}

效果图如下所示:

2.3 删除shortcuts

此处的代码实现如下所示:

  //给ListView添加长按事件进行删除shortcutlv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {@Overridepublic boolean onItemLongClick(AdapterView<?> adapterView, View view, int position, long id) {Toast.makeText(MainActivity.this, data.get(position) + "陨落了", Toast.LENGTH_SHORT).show();removeShortcut(position);return false;}});/*** 移除Shortcut** @param*/@TargetApi(Build.VERSION_CODES.N_MR1)private void removeShortcut(int index) {//获取已有快捷条目List<ShortcutInfo> infos = mShortcutManager.getPinnedShortcuts();for (ShortcutInfo info : infos) {if (info.getId().equals("id" + index)) {//使得标签失效mShortcutManager.disableShortcuts(Arrays.asList(info.getId()), "好汉陨落了");}}//通过标签id移除指定标签mShortcutManager.removeDynamicShortcuts(Arrays.asList("id" + index));}

效果图如下所示:

shortcut被设置为不可用之后,其移植在桌面上的快捷方式将会变成灰色。

静态使用的DEMO

动态使用的DEMO

对Shortcut使用的理解相关推荐

  1. 残差网络(Residual Network,ResNet)原理与结构概述

    残差网络是为了解决模型层数增加时出现梯度消失或梯度爆炸的问题而出现的.传统的神经网络中,尤其是图像处理方面,往往使用非常多的卷积层.池化层等,每一层都是从前一层提取特征,所以随着层数增加一般会出现退化 ...

  2. 深入理解shortcut

    概述 shortcut(或shortpath,中文"直连"或"捷径")是CNN模型发展中出现的一种非常有效的结构,本文将从Highway networks到Re ...

  3. 深入理解JavaScript系列(23):JavaScript与DOM(上)——也适用于新手

    文档对象模型Document Object Model DOM(Document Object Model,文档对象模型)是一个通过和JavaScript进行内容交互的API. Javascript和 ...

  4. Python字典理解

    本文翻译自:Python Dictionary Comprehension Is it possible to create a dictionary comprehension in Python ...

  5. 深入理解 Spring 之源码剖析IOC

    推荐两篇文章: https://www.jianshu.com/p/e4ca039a2272 https://www.cnblogs.com/ITtangtang/p/3978349.html 一.介 ...

  6. 理解Floyd-Warshall算法

    2019独角兽企业重金招聘Python工程师标准>>> 我们之前分别讨论了Dijkstra算法和Bellman-Ford算法,它们解决的都是单源最短路径问题.本文讨论的Floyd-W ...

  7. ACL 2021 | 为什么机器阅读理解模型会学习走捷径?

    ©PaperWeekly 原创 · 作者 | Maple小七 学校 | 北京邮电大学硕士生 研究方向 | 自然语言处理 虽然当前的 MRC 模型在不少阅读理解 benchmark 上接近甚至超越了人类 ...

  8. cnpm 网络不能连接_(二十七)通俗易懂理解——Resnet残差网络

    Resnet看相关的文章都比较容易理解,本文主要转自两篇对该内容有较为全面解释和理解的文章. 1. 引言 网络的深度为什么重要? 因为CNN能够提取low/mid/high-level的特征,网络的层 ...

  9. 理解浏览器是如何加载及渲染网页的

    先上图,我们再慢慢解释,这图就是浏览器加载网页的一个过程 当我们在浏览器输入一个地址(比如:http://toadw.cn),那么点击回车后,浏览器是如何加载网页的呢? 加载过程 一开始浏览器是不知道 ...

最新文章

  1. python 遍历目录_Python遍历目录的4种方法实例介绍
  2. IOC操作Bean管理XML方式(P名称空间注入)
  3. virtualbox 启动时报Kernel driver not installed (rc=-1908) 的错误
  4. 一起学习C语言:数组(二)
  5. Springboot+Mybatis+PageHelper 分页、排序
  6. 带你用Python玩转PPT
  7. 空军军医大学计算机复试线,2021空军军医大学考研国家线公布时间_国家线是多少分...
  8. GET 和 POST请求的本质区别是什么?看完觉得自己太无知了...
  9. 【课程总结】软件工程经济学简答题总结
  10. 将数字转换成对应的中文 将阿拉伯数字翻译成中文的大写数字
  11. 从mitt、tiny-emitter源码中学习手写订阅发布
  12. 有关信息安全的三个案例
  13. 清理工作区git clean -fd
  14. 项目启动报错No appropriate protocol (protocol is disabled or cipher suites are inappropriate) 解决办法
  15. Android chrisbanes-PhotoView 使用案例
  16. Jpa持久对象状态,一级缓存,二级缓存
  17. 穷查理宝典--人类误判心理学思维导图
  18. 关爱亚孤儿 我们在行动——九巨龙肥城市京欣学校走进聊城市东昌府区
  19. 坚果啊!ISA-NUT协会成立:求围观
  20. Jmeter之必备的正则表达式和正则表达式提取器

热门文章

  1. 6种常见的三维重建方式
  2. 图片相似度计算:深入理解DCT变换以及感知哈希
  3. 【系统辨识】最小二乘法
  4. java实现插入排序和希尔排序
  5. 在局域网内如何访问另一台计算机
  6. 应用ceph对象存储(ceph-13.2.10)
  7. mysql复合索引(联合索引)的使用场景
  8. EventBus的理解和使用
  9. 脑电分析系列[MNE-Python-20]| 脑电图处理和事件相关电位(ERP)
  10. zeppelin源码分析(0)——zeppelin要解决什么问题