在Android系统上,有时候会看到在桌面上长按应用图标弹出应用快捷列表,例如美团、支付宝、微博、印象笔记、QQ邮箱等,效果图如下所示:
用户可以通过这些快捷键,直接访问应用具体的界面,并且长按这些快捷键可以在桌面上创建一个该快捷键的应用图标,比如长按上图中的收钱快捷键,拖动即可在桌面创建一个支付宝收钱快捷方式。
这个效果是Android 7.1之后新增的一个功能:应用快捷方式ShortcutManager,官方API地址:
https://developer.android.google.cn/reference/android/content/pm/ShortcutManager.html,接下来,我们将演示该快捷方式的具体实现方法:
- 静态的: 在xml中定义, 适用于一些通用的动作.
- 动态的: 由ShortcutManager发布, 可以根据用户的行为或者偏好添加, 可以动态更新.
注:该快捷方式只能在Android 7.1的设备上显示,且每一个应用目前最多可以有5个shortcuts(静态 + 动态)。
静态ShortcutManager的使用
静态的Shortcuts是写在xml中的,除非应用更新,否则不能被修改。
首先在res/xml/创建一个shortcuts.xml文件,内容如下所示:
<?xml version="1.0" encoding="utf-8"?>
<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
<shortcut
android:enabled="true"
android:icon="@drawable/ic_search"
android:shortcutDisabledMessage="@string/lable_shortcut_static_search_disable"
android:shortcutId="shortcut_id_search"
android:shortcutLongLabel="@string/lable_shortcut_static_search_disable"
android:shortcutShortLabel="@string/lable_shortcut_static_search_disable">
<intent
android:action="android.intent.action.VIEW"
android:targetClass="com.cjxj.androiddemo.activity.MainActivity"
android:targetPackage="com.cjxj.androiddemo" />
<categories android:name="android.shortcut.conversation" />
</shortcut>
</shortcuts>
其中属性分别表示:
- shortcutId表示 shortcut 唯一标识符,相同的 shortcutId 会被覆盖。必须字段。
- shortcutShortLabel为将 shortcut 拖动到桌面时显示的名字,官方建议不超过 10 个字符,必须字段。
- shortcutLongLabel为 shortcut 列表中每个 shortcut 的名字,不宜过长,如果过长或未设置默认会显示 ShortLabel,官方建议不超过 25 个字符。可选字段。
- icon为 shortcut 的 icon,在列表展示和拖动到桌面时显示需要,可选字段。
- enabled表示 shortcut 是否可用,false 表示禁用。
- shortcutDisabledMessage为已固定在桌面的 shortcut 被 Disabled 后点击时的 Toast 提示内容。可选字段。
- intent为点击 shortcut 时响应的 Intent,必须字段。
这里可以添加多个 Intent,但点击时不会启动所有 Intent,而是启动最后一个 Intent,在这个 Intent 回退时会启动它前面一个 Intent,相当于自动将所有 Intent 添加到了堆栈。
然后在应用的Manifest文件中配置启动界面的Activity,内容如下:
<activity
android:name=".activity.HomeListActivity"
android:screenOrientation="portrait">
<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>
至此,我们就添加了一个搜索功能的快捷方式,效果如下:
动态ShortcutManager的使用
动态是通过 ShortcutManager API 进行操作,可以动态添加、修改、删除。
if (Build.VERSION.SDK_INT >= 25) {
ShortcutInfo shortcutInfo = new ShortcutInfo.Builder(this, "shortcut_id_search")
.setShortLabelResId(R.string.lable_shortcut_static_search_disable)
.setLongLabelResId(R.string.lable_shortcut_static_search_disable)
.setIcon(Icon.createWithResource(this, R.drawable.ic_search))
.setIntent(new Intent(this, MainActivity.class))
.build();
ShortcutManager shortcutManager = getSystemService(ShortcutManager.class);
//这样就可以通过长按图标显示出快捷方式了
shortcutManager.setDynamicShortcuts(Arrays.asList(shortcutInfo));
}
通过ShortcutInfo.Builder新建 ShortcutInfo,再通过shortcutManager添加即可。其中:
- setDynamicShortcuts(List)可以替换并添加所有 shortcut 列表;
- addDynamicShortcuts(List)可以添加新的 shortcut 到列表,超过最大个数会报异常;
- updateShortcuts(List)可以更新一组 shortcuts;
- removeDynamicShortcuts(List)和removeAllDynamicShortcuts() 可以删除部分或所有 shortcuts。
至此,关于应用快捷方式的使用就介绍结束了,如有问题,欢迎留言。