长按应用图标弹出快捷方式,这个效果是Android 7.1之后新增的一个功能:应用快捷方式ShortcutManager,
官方API地址: https://developer.android.google.cn/reference/android/content/pm/ShortcutManager.html
桌面上长按应用图标弹出应用快捷列表,用户可以通过这些快捷键,直接访问应用具体的界面,并且长按这些快捷键可以在桌面上创建一个该快捷键的应用图标。
创建方式分为静态和动态的两种方式:
静态的: 在xml中定义, 适用于一些通用的动作.
动态的: 由ShortcutManager发布, 可以根据用户的行为或者偏好添加, 可以动态更新.
注:该快捷方式Android 7.1,且每一个应用目前最多可以有5个shortcut。
一、静态创建:在资源文件下创建xml包,再生成一个xml文件
<?xml version="1.0" encoding="utf-8"?>
<shortcuts xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android">
<!--shortcutShortLabel:拖动到桌面时显示的名字,官方建议不超过 10 个字符,必须字段-->
<!--shortcutLongLabel:列表中每个shortcut的名字,不宜过长,如果过长或未设置默认会显示ShortLabel,官方建议不超过 25 个字符。可选字段。-->
<!--shortcutDisabledMessage:为已固定在桌面的 shortcut 被 Disabled 后点击时的 Toast 提示内容。可选字段-->
<shortcut
android:enabled="false"
android:icon="@drawable/ic_baby_head"
android:shortcutId="set"
android:shortcutLongLabel="@string/shortcut_long_set"
android:shortcutShortLabel="@string/shortcut_short_set"
android:shortcutDisabledMessage="@string/shortcut_long_set"
tools:targetApi="n_mr1">
<intent
android:action="android.intent.action.VIEW"
android:targetClass="com.shuniuyun.aiyoumi.ui.mine.SettingActivity"
android:targetPackage="com.shuniuyun.aiyoumi" />
</shortcut>
<shortcut
android:enabled="true"
android:icon="@drawable/ic_baby_head"
android:shortcutId="info"
android:shortcutLongLabel="@string/shortcut_long_info"
android:shortcutShortLabel="@string/shortcut_short_info"
android:shortcutDisabledMessage="@string/shortcut_msg_info"
tools:targetApi="n_mr1">
<intent
android:action="android.intent.action.VIEW"
android:targetClass="com.shuniuyun.aiyoumi.ui.mine.PersonalInfoActivity"
android:targetPackage="com.shuniuyun.aiyoumi" />
</shortcut>
<shortcut
android:enabled="true"
android:icon="@drawable/ic_baby_head"
android:shortcutId="username"
android:shortcutLongLabel="@string/shortcut_long_name"
android:shortcutShortLabel="@string/shortcut_short_name"
android:shortcutDisabledMessage="@string/shortcut_msg_name"
tools:targetApi="n_mr1">
<intent
android:action="android.intent.action.VIEW"
android:targetClass="com.shuniuyun.aiyoumi.ui.mine.ChangeUsernameActivity"
android:targetPackage="com.shuniuyun.aiyoumi" />
</shortcut>
</shortcuts>
然后在清单文件中进行配置(注意在启动页面下进行配置)
<activity android:name=".ui.main.StartActivity">
<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 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。