Shortcuts(快捷方式) Android7

2026-02-26 15:10:51

今天无意卸载应用:什么值得买、的时候发现这个功能,才知道是Shortcuts,然后看了手机中的app,QQ、QQ浏览器、网易云、高德地图、知乎、bibi时,发现这个功能

Shortcuts的介绍

其中App Shortcuts是指在桌面长按app图标而出现的快捷方式, 可以为你的app的关键功能添加更快速的入口而不用先打开app,点击快捷方式可以访问应用功能, 并且这种快捷方式也可以被拖拽到桌面单独放置, 变成单独的桌面快捷方式。

有两种shortcuts:

静态的: 在xml中定义,适用于一些通用的动作.

动态的: 由ShortcutManager发布, 可以根据用户的行为或者偏好添加, 可以动态更新.

数量

每一个应用目前最多可以有5个shortcuts(静态 + 动态)

运行条件

应用添加App Shortcuts是Android 7.1(API 25)的API,所以只能在Android 7.1的设备上显示, 同时需要launcher支持, 比如Pixel launcher(Pixel设备的默认launcher),Now launcher(Nexus设备上的launcher)现在就支持, 其他launcher也可以提供支持。

静态的shortcuts的使用

什么是Static Shortcuts? 我的理解就是利用xml写死的配置。想想BroadcastReceiver可以静态注册也可以利用java代码动态注册,这里也是一样的。

那静态注册该如何做呢? 首先, 我们需要在res/xml目录下创建一个新的xml文件, 这里我们命名为shortcuts.xml

android:enabled="true"

android:icon="@drawable/got"

android:shortcutDisabledMessage="@string/settings_disable_msg"

android:shortcutId="settings"

android:shortcutLongLabel="@string/settings_long_name"

android:shortcutShortLabel="@string/settings_short_name">

android:action="android.intent.action.VIEW"

android:targetClass="com.ranran.myapplication.Main2Activity"

android:targetPackage="com.ranran.myapplication" />

首先一个shortcuts标签, 然后是一个shortcut, 到这里我们大概可以猜测到这里可以注册多个shortcut, shortcut标签有很多属性, 我们来一个个的了解下.

shortcutId, 不用多说, 这肯定是一个唯一的id

enabled, 表示这个shortcut是否可用

shortcutShortLabel, 这里是配置的短名称, 下面还会有长名称, 如果长名称显示不下, 就显示短名称

shortcutLongLabel, 这里是配置的长名称, launcher会优先选择长名称显示

shortcutDisabledMessage, 这个配置是在我们选择一个不可用的shortcut时给用户的一个提示

在shortcut标签下还有两个我们熟悉的标签.

intent, 这里表示我们点击shortcut时要干嘛, t

argetPackage是指定一个目标应用的包名, targetClass是我们要跳转的目标类, 这里要注意的是android:action一定要配置, 否则会崩溃

categories, 这个东西目前位置官方只给提供了android.shortcut.conversation

注意,shortcutLongLabel和shortcutShortLabel,不可以直接引用文字,不然会报错.!!!

ok, 上面的几行代码, 我们一个static shortcuts就完成了, 那如何使用呢? 是在manifest中配置activity的地方使用, 而且这个activity是有要求的.

能配置shortcuts的activity必须要有action是android.intent.action.MAIN和category是android.intent.category.LAUNCHER

也就是说shortcuts引用在主界面 至于为什么要有这个要求, 自己去体会......

android:name="android.app.shortcuts"

android:resource="@xml/shortcuts" />

到这里, 静态配置shortcuts我们就学习完了, 是不是很简单? 那这个静态配置是用在什么地方呢? 我想了想, 这里适用的场景一般是一些固定不变的功能, 例如你APP的设置界面, 如果是一些动态的数据, 那静态配置就不适合了, 就需要我们接下来要介绍到了动态配置了。

使用Dynamic Shortcuts(动态)

动态的shortcuts可以在用户使用app的过程中构建, 更新, 或者删除。

ShortcutManager

设置或者新增 setDynamicShortcuts; addDynamicShortcuts;

修改 updateShortcuts;

删除 removeDynamicShortcuts; removeAllDynamicShortcuts;

动态添加三个

/**

* 动态添加三个

*/

@RequiresApi(api = Build.VERSION_CODES.N_MR1)

private void getNewShortcutInfo() {

ShortcutInfo shortcut = new ShortcutInfo.Builder(this, "id1")

.setShortLabel("Web site")

.setLongLabel("第一个")

.setIcon(Icon.createWithResource(this, R.drawable.player))

.setIntent(new Intent(Intent.ACTION_VIEW,

Uri.parse("https://www.baidu.com/")))

.build();

ShortcutInfo shortcut2 = new ShortcutInfo.Builder(this, "id2")

.setShortLabel("Web site")

.setLongLabel("我的Github")

.setIcon(Icon.createWithResource(this, R.drawable.player))

.setIntent(new Intent(Intent.ACTION_VIEW,

Uri.parse("https://github.com/growuptang")))

.build();

ShortcutInfo shortcut3 = new ShortcutInfo.Builder(this, "id3")

.setShortLabel("Web site")

.setLongLabel("我的CSDN")

.setIcon(Icon.createWithResource(this, R.drawable.player))

.setIntent(new Intent(Intent.ACTION_VIEW,

Uri.parse("https://blog.csdn.net/qq_33721320")))

.build();

mShortcutManager.setDynamicShortcuts(Arrays.asList(shortcut, shortcut2, shortcut3));

}

在onCreate中调用

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {

mShortcutManager = getSystemService(ShortcutManager.class);

getNewShortcutInfo();

}

效果如下

书到用时方恨少,纸上得来终觉浅。祝君好运!

参考链接

「捷徑」Shortcuts App

[桌面] 来聊聊桌面Shortcuts应用快捷功能