Android 7.1 新功能之一就是 App Shortcuts(应用快捷方式) ,该功能与 iPhone 上的 3D Touch 功能相似,通过长按应用图标,可弹出应用快捷方式,点击可以直接跳转到相应的界面。目前最多支持 5 个快捷方式,可以 getMaxShortcutCountPerActivity() 查看 Launcher 最多支持几个快捷方式,不同的是 Android 支持通过拖拽将快捷方式固定到桌面。

看似美好,其实应用快捷方式还是有很多缺陷的:

  1. 只能在 Google 的 Nexus 及 Pixel 设备上使用
  2. 系统必须是 Android 7.1 及以上(API Level >= 25)
  3. 已经被用户固定到桌面的快捷方式必须得到兼容性处理,因为你基本上失去了对其控制,除了升级时禁用

Launcher applications allow users to "pin" shortcuts so they're easier to access. Both manifest and dynamic shortcuts can be pinned. Pinned shortcuts cannot be removed by publisher applications; they're removed only when the user removes them, when the publisher application is uninstalled, or when the user performs the "clear data" action on the publisher application from the device's Settings application.
However, the publisher application can disable pinned shortcuts so they cannot be started. See the following sections for details.

应用快捷方式可分为 Static Shortcuts(静态快捷方式) 和 Dynamic Shortcuts(动态快捷方式) 两种。

  • 静态快捷方式:又名 Manifest Shortcuts,在应用安装时创建,不能实现动态修改,只能通过应用更新相应的 XML 资源文件才能实现更新。
  • 动态快捷方式:应用运行时通过 ShortcutManager 实现动态添加、删除、禁用等操作。

下面分别来讲述如何创建静态快捷方式和动态快捷方式。

创建静态快捷方式