android 7.0新特性,类似iPhone的3Dtouch,长按app桌面图标会弹出快捷方式,效果如下:
引入快捷方式:Shortcuts
两种方式:静态xml和动态java设置(类似BroadcastReceiver)
静态使用步骤:
1.在工程资源文件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/amap_car"
android:shortcutDisabledMessage="@string/disable_tip"
android:shortcutId="setting"
android:shortcutLongLabel="@string/full_name"
android:shortcutShortLabel="@string/short_name">
<intent
android:action="android.intent.action.VIEW"
android:targetClass="com.lagou.ktdemo.firstkotlin.JavaActivity"
android:targetPackage="com.lagou.ktdemo.firstkotlin" />
<categories android:name="android.shortcut.conversation" />
</shortcut>
</shortcuts>
shortcuts标签下可以注册多个shortcut,
shortcut的常用属性如下:
- 1.shortcutId, 唯一的id
- enabled, 表示该shortcut是否可用
- shortcutShortLabel, 短名称, 下面还会有长名称, 如果长名称显示不下, 就显示短名称
- shortcutLongLabel, 长名称, launcher会优先选择长名称显示
- shortcutDisabledMessage, 当用户选择一个不可用的shortcut时给的toast提示内容
intent表示我们点击shortcut时的意图, 属性说明:
-
targetPackage是指定一个目标应用的包名,
-
targetClass是我们要跳转的目标类,
-
action记得一定要配置, 否则会崩溃
-
categories, 目前官方只提供了android.shortcut.conversation,写死即可。
2.在清单文件中配置shortcuts:
注意,配置shortcuts的activity必须要具备action是android.intent.action.MAIN和category是android.intent.category.LAUNCHER!
所以清单文件应该是这样的:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.lagou.ktdemo.firstkotlin">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity"></activity>
<activity android:name=".JavaActivity">
<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>
静态配置,基本就是这样。显然这样不够灵活,我们可以根据后台数据动态配置。
动态配置:Dynamic Shortcuts
private ShortcutManager mShortcutManager;
private void setupShortcuts() {
mShortcutManager = getSystemService(ShortcutManager.class);
//获取已静态配置的列表
List<ShortcutInfo> manifestShortcuts = mShortcutManager.getManifestShortcuts();
int xmlSize = manifestShortcuts.size();
//shortcut并非可以无限添加,先获取配置最大数
int maxShortcutCount = mShortcutManager.getMaxShortcutCountPerActivity();
int enableCount = maxShortcutCount - xmlSize;
//待添加shortcutInfo列表
List<ShortcutInfo> infoList = new ArrayList<>(enableCount);
for (int i = 0; i < enableCount; i++) {
Intent intent = new Intent(this, JavaActivity.class);
intent.setAction(Intent.ACTION_VIEW);
ShortcutInfo info = new ShortcutInfo.Builder(this, "id" + i)
.setShortLabel("简称" + i)
.setLongLabel("联系人名字很长:" + i)
.setIcon(Icon.createWithResource(this, R.drawable.amap_man))
.setIntent(intent)
.build();
infoList.add(info);
}
mShortcutManager.setDynamicShortcuts(infoList);
}
动态添加,这样基本就完成了,下面讲述如何动态删除?
shortcut除了长按时候可以列表形式展示在icon上方,也可以把单个item拖到桌面。类似这样
显然,动态删除,不但考虑删除列表,还要删除已放置桌面的快捷入口icon,但是文档提到只允许用户手动删除icon,那么我们删除与之对应的Activity后,用户再点击快捷入口icon会不会崩溃呢?显然系统提供了对策。
Pinning Shortcuts就是我们提到的桌面快捷入口icon。
动态删除:
private void removeShortcut(String id) {
List<ShortcutInfo> infos = mShortcutManager.getPinnedShortcuts();
for (int i = 0; i < infos.size(); i++) {
if (id.equals(infos.get(i).getId())) {
mShortcutManager.disableShortcuts(Arrays.asList(id), "暂不可用");
}
}
mShortcutManager.removeDynamicShortcuts(Arrays.asList(id));
}
- 首先我们先调用mShortcutManager.getPinnedShortcuts()来获取到所有的Pinning Shortcuts, 找到我们删除的那个,
- 然后通过disableShortcuts(List)来禁用掉该项,
- 最后我们还要用过removeDynamicShortcuts(List)来从shortcuts中移除。
动态更新 信息:
private void updateShortcut(int index) {
Intent intent = new Intent(this, JavaActivity.class);
intent.setAction(Intent.ACTION_VIEW);
intent.putExtra("msg", "some info....");
ShortcutInfo info = new ShortcutInfo.Builder(this, "id" + index)
.setShortLabel("家")
.setLongLabel("地址很长的")
.setIcon(Icon.createWithResource(this, R.drawable.address))
.setIntent(intent)
.build();
mShortcutManager.updateShortcuts(Arrays.asList(info));
}
总结:
-
类似于 iOS 的 3D Touch,长按启动图标弹出几个快捷入口,入口最好不要超过 4 个,像搜索、扫描二维码、发帖等应用程序最常用功能的入口被称为静态 shortcut,不会随着用户不同或随着用户使用而改变。
还有一种像从某个存档点继续游戏、任务进度等与用户相关的上下文敏感入口被称为动态 shortcut,会因用户不同或随着用户使用不断变化。还有一种在 Android 8.0 (API level 26) 及以上系统版本上像固定网页标签等用户主动固定到桌面的快捷方式被称为固定 shortcut -
静态 shortcut 系统可以自动备份和恢复,动态 shortcut 需要应用自己备份和恢复,固定 shortcut 的图标系统无法备份和恢复因此需要应用自己完成
-
android:shortcutId 和 android:shortcutShortLabel 属性是必须的,android:shortcutShortLabel 不能超过 10 个字符,android:shortcutLongLabel 不能超过 25 个字符,android:icon 不能包含 tint
-
获取 ShortcutManager 的方式有两个: getSystemService(ShortcutManager.class) 和 getSystemService(Context.SHORTCUT_SERVICE)