在看完Static Shortcuts后, 我们不相信Google仅仅给我们开发者开放了局限性如此大的使用方式, 肯定还会存在灵活性更大的API, 是的, 这就是我们马上要讲的Dynamic Shortcuts, 我把它称为动态配置.
说起动态配置, 那肯定是用java代码实现了, 那如何实现呢?
首先第一步, 我们需要利用一下代码拿到ShortcutManager
getSystemService(ShortcutManager.class)
拿到ShortcutManager后, 我们可以调用setDynamicShortcuts(List)方法去设置Shortcut, 那这个List如何得到呢? 我们来看看完整点的代码,
private void setupShortcuts() {
mShortcutManager = getSystemService(ShortcutManager.class);
List<ShortcutInfo> infos = new ArrayList<>();
for (int i = 0; i < mShortcutManager.getMaxShortcutCountPerActivity(); i++) {
Intent intent = new Intent(this, MessageActivity.class);
intent.setAction(Intent.ACTION_VIEW);
intent.putExtra("msg", "我和" + mAdapter.getItem(i) + "的对话");
ShortcutInfo info = new ShortcutInfo.Builder(this, "id" + i)
.setShortLabel(mAdapter.getItem(i))
.setLongLabel("联系人:" + mAdapter.getItem(i))
.setIcon(Icon.createWithResource(this, R.drawable.icon))
.setIntent(intent)
.build();
infos.add(info);
// manager.addDynamicShortcuts(Arrays.asList(info));
}
mShortcutManager.setDynamicShortcuts(infos);
}
这段代码的背景是我们模拟了一个联系人列表功能, 在launcher中我们长按图标会出现一定数量的联系人快捷方式, 点击某个快捷方式会直接跳转该联系人相关的页面. 好, 介绍完背景, 我们来看代码, 首先我们通过getSystemService(ShortcutManager.class)来拿到ShortcutManager, 接下来一个for循环, 注意这个for循环的次数, 因为我们要添加的Shortcut不能是无限个, 所以这里我们用getMaxShortcutCountPerActivity来获取到最大个数. 然后在for循环里, 我们首先构造一个intent, 注意, 这里和Static Shortcut一样, 必须要提供一个Action. 然后我们用ShortcutInfo.Builder来构造一个ShortcutInfo并且放到List中, 最终我们调用mShortcutManager.setDynamicShortcuts(infos)来设置Shortcuts.
好了, 代码其实很简单, 我们来看看效果.
是不是很666?
动态更新 Shortcuts
上面的代码我们虽然说是Dynamic, 但仅仅是使用java代码实现的罢了, 真正的Dynamic我们接下来才去讲解, 在讲解Dynamic之前, 我们先来介绍一个名词-Pinning Shortcuts, 这是个啥玩意呢? 其实对于Shortcut, Android是允许我们直接放到桌面的, 这样就更加方便了用户的操作, google把他称作为Pinning Shortcuts, 具体啥样, 我们来张图就明白了.
对于这个Pinning Shortcuts, google的文档说, 我们开发者是没有权利去删除的, 能删除它的只有用户. 那我该项功能删除了咋办? 这东西还在桌面上, 是不是APP要崩? 当然Google考虑到了这点, 它允许我们去disable这个shortcut. 具体还是来看代码, 这里我们长按item来模拟一下删除.
private void removeItem(int index) {
List<ShortcutInfo> infos = mShortcutManager.getPinnedShortcuts();
for (ShortcutInfo info : infos) {
if (info.getId().equals("id" + index)) {
mShortcutManager.disableShortcuts(Arrays.asList(info.getId()), "暂无该联系人");
}
}
mShortcutManager.removeDynamicShortcuts(Arrays.asList("id" + index));
}
首先我们先调用mShortcutManager.getPinnedShortcuts()来获取到所有的Pinning Shortcuts, 然后去遍历它, 找到我们删除的那个, 然后通过APIdisableShortcuts(List)来disable掉该项, 最后我们还要用过removeDynamicShortcuts(List)来从shortcuts中移除. 来看看效果.
通过效果中, 我们可以看到, 我们disableShortcuts的那个Pinning Shortcut已经变灰了, 而且在点击的时候会提醒暂无该联系人, 这个提醒正是disableShortcuts的第二个参数.
现在, 删除和禁用我们已经了解了, 那更新呢? 假如我修改了某个联系人的名字, shortcut是不是也应该相应的修改呢? 是的, 这里还是需要我们通过代码来实现.
private void updItem(int index) {
Intent intent = new Intent(this, MessageActivity.class);
intent.setAction(Intent.ACTION_VIEW);
intent.putExtra("msg", "我和" + mAdapter.getItem(index) + "的对话");
ShortcutInfo info = new ShortcutInfo.Builder(this, "id" + index)
.setShortLabel(mAdapter.getItem(index))
.setLongLabel("联系人:" + mAdapter.getItem(index))
.setIcon(Icon.createWithResource(this, R.drawable.icon))
.setIntent(intent)
.build();
mShortcutManager.updateShortcuts(Arrays.asList(info));
}
构建intent我们就不说了, 接下来我们又使用ShortcutInfo.Builder来构建了一个新的ShortcutInfo, 最后我们是用过updateShortcuts(List)来实现更新shortcut的, 很简单, 来看看效果.
ok, 现在, Android7.1的Shortcuts功能我们就差不多介绍完了, 文章中的实例代码大家可以在https://github.com/qibin0506/Android7_Shortcuts_Demo上下载到, 官网的文档大家也可以多看看, 这里给出地址: https://developer.android.com/preview/shortcuts.html
最后的彩蛋:大家都知道, 从android 7开始, google开始推广圆形图标了, 那肯定有很多人疑惑“那我在之前的launcher上咋办? 很丑陋啊”, 其实这点google完全考虑到了,圆形图标并不是通过android:icon指定的, 是一个额外提供的属性支持圆形图标的:android:roundIcon, 所以说,现在我们的manifest应该这么写了:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.commitcontent.app">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:roundIcon="@mipmap/ic_launcher_round"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
看application的配置, android:icon指定普通图标, android:roundIcon指定的是圆形图标, 那么在支持圆形图标的launcher上就会显示圆形图标, 我们现在大部分人用的launcher上还是显示默认的图标。