有的App会给用户提供快捷方式,8.0之前我们多是通过发送com.android.launcher.action.INSTALL_SHORTCUT 广播的方式来实现。但在8.0的系统行为变更中指出,该广播变为私有的隐式广播,因此原来创建快捷方式的方法在8.0的系统上将不再有效,官方推荐我们使用ShortcutManager来创建应用快捷方式。

快捷方式应用场景及类型

快捷方式应用场景

Android 7.1起,开发者可以创建一些跳转到特定页面的快捷方式,常见的应用场景如:

  1. 地图App中为用户导航到特定位置
  2. 社交App中向某个好友发送消息
  3. 多媒体App中播放电视剧的下一集
  4. 游戏App中加载上次保存的状态

快捷方式类型

Static shortcuts

静态快捷方式通过资源文件定义,并且打包到APK中,因此只能在更新App时才能修改此类快捷方式。

Dynamic shortcuts

动态快捷方式在App运行时使用ShortcutManager API定义,可以在运行时创建、修改和删除。

Pinned shortcuts

固定快捷方式也是在App运行时通过ShortcutManager API定义,App会弹出确认对话框来申请创建该快捷方式,如果用户允许,则此快捷方式将会显示在支持它的启动器(Launcher)上。

一个App最多可以创建5个静态、动态快捷方式,对于固定快捷方式的数量则没有限制,但一些Launcher应用可能不会全部显示出所创建的动态或静态快捷方式。

注意:虽然其他应用无法访问快捷方式的元数据,但Launcher应用可以,因此创建快捷方式时尽量不要涉及用户隐私。

使用静态快捷方式

应用场景

  1. 查看发送的消息
  2. 设置闹钟
  3. 显示用户当天的活动

创建步骤

  1. 在AndroidManifest.xml中,找到启动Activity,即设置了 android.intent.action.MAINandroid.intent.category.LAUNCHER这两个Intent Filter的Activity。

  2. 为这个Activity设置元数据,并引用为其定义的快捷方式资源文件,如下:

    <manifest xmlns:android="http://schemas.android.com/apk/res/android"package="com.example.myapplication"><application ... ><activity android:name="Main"><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></application>
    </manifest>
  3. 创建快捷方式的资源文件 res/xml/shortcuts.xml,其根元素为 <shortcuts>,它包含了一个快捷方式列表,每个快捷方式用 <shortcut>标签表示,每个快捷方式中又声明了快捷方式图标、描述、intent等信息,示例如下:

    <shortcuts xmlns:android="http://schemas.android.com/apk/res/android"><shortcut
       android:shortcutId="compose"android:enabled="true"android:icon="@drawable/compose_icon"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.myapplication"android:targetClass="com.example.myapplication.ComposeActivity" /><!-- If your shortcut is associated with multiple intents, include themhere. The last intent in the list determines what the user sees whenthey launch this shortcut. --><categories android:name="android.shortcut.conversation" /></shortcut><!-- Specify more shortcuts here. -->
    </shortcuts>

    注意:如果某个快捷方式配置了多个Intent,则点击快捷方式时会跳到最后一个Intent对应的界面,然后按回退栈的方式,如果点击返回键,则返回倒数第二个Intent对应的界面,跟startActivities(Intent[] intents)效果类似。

  4. 效果图,有点像iOS的3D Touch(来自官方文档)

使用动态快捷方式

应用场景

  1. 给某人打电话
  2. 导航到某个位置
  3. 查看游戏的当前分数

创建方法

  1. Publish: 使用 setDynamicShortcuts() 来重定义快捷方式列表,或者使用 addDynamicShortcuts() 来扩展已经存在的快捷方式列表。
  2. Update: 使用 updateShortcuts() 方法更新快捷方式。
  3. Remove: 使用 removeDynamicShortcuts()来删除一组快捷方式,或者使用removeAllDynamicShortcuts()来删除所有的快捷方式。

创建动态快捷方式的代码示例:

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));

使用固定快捷方式

应用场景

在8.0或更高的系统上可以使用固定快捷方式,此类快捷方式在支持它的Launcher上表现为独立的图标,如图(来自官网)所示:

当尝试创建此种快捷方式时,会弹出申请对话框让用户确定,如果用户没有同意,则Launcher会取消创建快捷方式的请求。

创建步骤

  1. 首先使用 isRequestPinShortcutSupported() 方法来检查设备默认的Launcher应用是否支持固定快捷方式。

  2. 创建ShortcutInfo 对象,根据快捷方式是否存在有两种不同的处理方式:

    1. 如果快捷方式已经存在,则创建的ShortcutInfo 对象仅包含快捷方式ID,系统会将其他信息自动关联到此快捷方式。
    2. 如果快捷方式不存在,则创建的ShortcutInfo 对象包含快捷方式ID、Intent、label等信息。
  3. 调用requestPinShortcut()方法尝试在设备Launcher上创建快捷方式,同时可以传入一个PendingIntent,当创建快捷方式成功时会通知应用。

    注意:如果用户不允许创建快捷方式,则应用不会收到回调。当快捷方式创建好之后,可以使用 updateShortcuts() 方法更新其内容。

示例代码:

ShortcutManager mShortcutManager =context.getSystemService(ShortcutManager.class);if (mShortcutManager.isRequestPinShortcutSupported()) {// Assumes there's already a shortcut with the ID "my-shortcut".// The shortcut must be enabled.ShortcutInfo pinShortcutInfo =new ShortcutInfo.Builder(context, "my-shortcut").build();// Create the PendingIntent object only if your app needs to be notified// that the user allowed the shortcut to be pinned. Note that, if the// pinning operation fails, your app isn't notified. We assume here that the// app has implemented a method called createShortcutResultIntent() that// returns a broadcast intent.Intent pinnedShortcutCallbackIntent =mShortcutManager.createShortcutResultIntent(pinShortcutInfo);// Configure the intent so that your app's broadcast receiver gets// the callback successfully.PendingIntent successCallback = PendingIntent.getBroadcast(context, 0,pinnedShortcutCallbackIntent, 0);mShortcutManager.requestPinShortcut(pinShortcutInfo,successCallback.getIntentSender());
}

兼容老版本

为了支持Android 7.1(API Level=25)及之前的版本,可以使用Android兼容库,参考ShortcutManagerCompat。

Android应用快捷方式相关推荐

  1. 每个人都应该知道的Android Studio快捷方式

    Android Studio has a lot of shortcuts to offer. Today, we will be sharing some useful shortcuts whic ...

  2. Android 应用快捷方式 Shortcuts(二)

    Android 应用快捷方式 Shortcuts(二) 简介: 快捷方式可帮助用户快速访问您的应用的某些部分,从而为他们呈现特定类型的内容.一次最多可以为应用发布五个快捷方式(静态和动态快捷方式加在一 ...

  3. Android 应用快捷方式 Shortcuts(一)

    Android 应用快捷方式 Shortcuts 简介: 快捷方式可帮助用户快速访问您的应用的某些部分,从而为他们呈现特定类型的内容.一次最多可以为应用发布五个快捷方式(静态和动态快捷方式加在一起), ...

  4. 关于android创建快捷方式会启动两个应用的问题(一)

     在做创建应用快捷方式时遇到两个问题: 一.创建快捷方式OK,但测试时MOTO部分机型会报错,原因也在Log里面给提示,如下: [java] view plaincopyprint? java.l ...

  5. android脚本快捷方式,Android:如何创建主屏幕快捷方式启动shell脚本?

    答案:您的问题的答案应该是GScript(开放源代码和"根除设备上的任何地方"),但是当脚本完成时,谁想要盯着该模态终端输出屏幕?详情如下. > SManager (free ...

  6. linux android studio 快捷方式,Android studio中的代码格式化快捷方式

    对于在Android Studio中自动格式化Java / xml文件,请使用以下命令: 正如您正在寻找与eclipse一样的单点工作,Android工作室中没有这样的内置功能,但它使您能够通过宏执行 ...

  7. android 代码删除快捷方式,Android创建快捷方式以及删除快捷方式

    /** * * 创建快捷方式 * @param map 快捷方式图标 * @param appName 快捷方式标题 * @param appUrl 快捷方式打开的地址 * @param iconUr ...

  8. android 桌面快捷方式,Android应用开发之(如何自动在桌面创建快捷方式)

    一般来说在 Android 中添加快捷方式的有以下两种: 在launcher的应用程序列表上,长按某一应用程序图标创建快捷方式到桌面 在桌面上长按在弹出框中选择快捷方式->应用程序->将添 ...

  9. android 查询快捷方式,Android编程添加快捷方式(Short)到手机桌面的方法(含添加,删除及查询)...

    本文实例讲述了Android编程添加快捷方式(Short)到手机桌面的方法.分享给大家供大家参考,具体如下: 权限 要在手机桌面上添加快捷方式,首先需要在manifest中添加权限. ? 添加快捷方式 ...

  10. Android shortcuts快捷方式

    创建快捷方式 快捷方式可帮助用户快速访问您的应用的某些部分,从而为他们呈现特定类型的内容. 您要如何以快捷方式来呈现内容取决于您的用例,以及快捷方式的上下文是属于应用驱动型还是用户驱动型.虽然静态快捷 ...

最新文章

  1. 教你如何编写第一个爬虫
  2. server环境信息【C#代码获取】
  3. __tostring和__invoke 方法
  4. 容器服务kubernetes弹性伸缩高级用法
  5. JavaEE基础(06):Servlet整合C3P0数据库连接池
  6. 负载均衡SLB中开启会话保持并选择重写Cookie时的配置方法
  7. vue获取原生html节点,vue是如何获取元素节点 ?
  8. 错误摘要 HTTP 错误 403.14 - Forbidden Web 服务器被配置为不列出此目录的内容。
  9. 腾讯面试题: 百度搜索为什么那么快? | 原力计划
  10. Python入门--元组的遍历,获取元组元素
  11. [7]2019-ICDM-Learning to Sample:an Active Learning Framework 论文笔记
  12. Git 报错:error: The requested URL returned error: 401 Unauthorized while accessing
  13. 非极大值抑制(NMS)讲解
  14. PDF预览之PDFObject.js总结
  15. 航空公司客户价值数据的分析
  16. 软件系统演示脚本实践(草稿)
  17. java计算机毕业设计英语学习网站设计与实现MyBatis+系统+LW文档+源码+调试部署
  18. MMORPG游戏服务器技术选型参考
  19. uni-app学习:CSS之渐变色
  20. 保存相片是什么图像格式?

热门文章

  1. 用123 组成的6位数 的java代码,输入一个三位的整数,计算其每位数字的累加之和。例如:输入整数123后,其每位数字累加之和为6(1+2+3),感激不尽...
  2. Python系列9——类和对象(3)
  3. 获得bat的运行目录
  4. python的list的基本操作、list循环、切片、字典基本操作、字典嵌套、字符串常用方法...
  5. MATLAB伽罗域是什么,利用伽罗华域的运算来计算生成矩阵
  6. python- 小猫钓鱼纸牌游戏
  7. 受益于人工智能的发展,人们可以以前所未有的时空分辨率系统地观察神经活动
  8. 福禄克气体流量校准解决方案
  9. 虚拟化技术——虚拟存储
  10. 接口文档系统 - Yapi