ShortCuts是什么?
Shortcuts是指在桌面长按app图标而出现的快捷方式,可以为你的app的关键功能添加更加快速的入口而不是先打开app
点击快捷方式可以访问应用功能,而且这种快捷方式也可以被拖拽到桌面的单独位置,变成单独的左面快捷方式。
添加ShortCuts的方式
有两种方式:
1.静态方式:在xml中定义,适用于一些通用操作
2.动态方式:由ShortcutManager发布,可以根据用户的行为或者偏好添加,可以动态更新
一些限制条件:
1.每个应用目前最多可以有5个shortcuts(包括静态和动态)
2.shortcuts是android7.1(api25)的api,所以只能在android7.1的设备上才能正常显示,同时需要launcher支持,nexus系列手机和pixel系列手机已经支持了。
动态添加方式
动态添加方式比较灵活,可以适用的场景比较丰富,一般开发过程中用到的机会比较多。这种方式可以再用户适用app的过程中构建,更新,或者删除。
-发布:setDynamicShortcuts,addDynamicShortcuts
-更新:updateShortcuts
-删除:removeDynamicShortcuts,removeAllDynamicShortcuts
ShortcutManager还有其他操作方法,代码如下:
public boolean setDynamicShortcuts(List<ShortcutInfo> shortcutInfoList) {throw new RuntimeException("Stub!");}public List<ShortcutInfo> getDynamicShortcuts() {throw new RuntimeException("Stub!");}public List<ShortcutInfo> getManifestShortcuts() {throw new RuntimeException("Stub!");}public boolean addDynamicShortcuts(List<ShortcutInfo> shortcutInfoList) {throw new RuntimeException("Stub!");}public void removeDynamicShortcuts(List<String> shortcutIds) {throw new RuntimeException("Stub!");}public void removeAllDynamicShortcuts() {throw new RuntimeException("Stub!");}public List<ShortcutInfo> getPinnedShortcuts() {throw new RuntimeException("Stub!");}public boolean updateShortcuts(List<ShortcutInfo> shortcutInfoList) {throw new RuntimeException("Stub!");}public void disableShortcuts(List<String> shortcutIds) {throw new RuntimeException("Stub!");}public void disableShortcuts(List<String> shortcutIds, CharSequence disabledMessage) {throw new RuntimeException("Stub!");}public void enableShortcuts(List<String> shortcutIds) {throw new RuntimeException("Stub!");}public int getMaxShortcutCountPerActivity() {throw new RuntimeException("Stub!");}public boolean isRateLimitingActive() {throw new RuntimeException("Stub!");}public int getIconMaxWidth() {throw new RuntimeException("Stub!");}public int getIconMaxHeight() {throw new RuntimeException("Stub!");}public void reportShortcutUsed(String shortcutId) {throw new RuntimeException("Stub!");}
demo代码:
ShortcutManager shortcutManager = getSystemService(ShortcutManager.class);ShortcutInfo shortcut1 = new ShortcutInfo.Builder(this,"id1").setShortLabel("short label1").setLongLabel("long label1").setRank(4).setIcon(Icon.createWithResource(this, R.mipmap.ic_launcher)).setIntent(new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.baidu.com"))).build();ShortcutInfo shortcut2 = new ShortcutInfo.Builder(this,"id2").setShortLabel("short label2").setLongLabel("long label2").setRank(3).setIcon(Icon.createWithResource(this, R.mipmap.ic_launcher)).setIntent(new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.baidu.com"))).build();ShortcutInfo shortcut3 = new ShortcutInfo.Builder(this,"id3").setShortLabel("short label3").setLongLabel("long label3").setRank(2).setIcon(Icon.createWithResource(this, R.mipmap.ic_launcher)).setIntent(new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.baidu.com"))).build();ShortcutInfo shortcut4 = new ShortcutInfo.Builder(this,"id4").setShortLabel("short label4").setLongLabel("long label4").setRank(1).setIcon(Icon.createWithResource(this, R.mipmap.ic_launcher)).setIntent(new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.baidu.com"))).build();ShortcutInfo shortcut5 = new ShortcutInfo.Builder(this,"id5").setShortLabel("short label5").setLongLabel("long label5").setRank(2).setIcon(Icon.createWithResource(this, R.mipmap.ic_launcher)).setIntent(new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.baidu.com"))).build();shortcutManager.setDynamicShortcuts(Arrays.asList(shortcut1,shortcut2,shortcut3,shortcut4,shortcut5));
注:关于shortcuts数量的问题,虽然名义上能添加5个,添加第6个的时候会抛出异常,但是只会显示4个,用setRank能改变显示的顺序(从小到大,不支持负数,数越小,离icon约近)
使用过程中处理back的问题,即多个Intent构建back stack的问题
ShortcutInfo shortcut5 = new ShortcutInfo.Builder(this,"id5").setShortLabel("short label5").setLongLabel("long label5").setRank(2).setIcon(Icon.createWithResource(this, R.mipmap.ic_launcher)).setIntents(new Intent[]{new Intent(Intent.ACTION_MAIN, Uri.EMPTY, this, MainActivity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK),new Intent(Main2Activity.ACTION_SS)}).build();
<activity
android:name=".Main2Activity"android:label="@string/title_activity_main2"android:theme="@style/AppTheme.NoActionBar"><intent-filter><action android:name="com.dahuatech.shortcuts.ddd"/><category android:name="android.intent.category.DEFAULT"/></intent-filter></activity>
注:target activity必须要用action启动
静态方式
<activity android:name=".MainActivity"><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>
<?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_check_circle_black_24dp"android:shortcutDisabledMessage="@string/static_shortcut_disabled_message"android:shortcutId="static"android:shortcutLongLabel="@string/static_shortcut_long_label_1"android:shortcutShortLabel="@string/static_shortcut_short_label_1"><intent
android:action="android.intent.action.VIEW"android:targetClass="com.ddmeng.hellonougat.shortcuts.StaticShortcutActivity"android:targetPackage="com.ddmeng.hellonougat" /></shortcut><shortcut
android:enabled="true"android:icon="@drawable/ic_android_black_24dp"android:shortcutDisabledMessage="@string/static_shortcut_disabled_message"android:shortcutId="static_2"android:shortcutLongLabel="@string/static_shortcut_long_label_2"android:shortcutShortLabel="@string/static_shortcut_short_label_2"><intent
android:action="android.intent.action.MAIN"android:targetClass="com.ddmeng.hellonougat.MainActivity"android:targetPackage="com.ddmeng.hellonougat" /><intent
android:action="com.ddmeng.hellonougat.action.STATIC_SHORTCUT_2"android:targetClass="com.ddmeng.hellonougat.shortcuts.StaticShortcutActivity"android:targetPackage="com.ddmeng.hellonougat" /></shortcut>
</shortcuts>
上面这个文件里添加了两个静态的shortcuts, 第一个关联了一个Activity, 点击shortcut将直接打开这个Activity, 回退的时候回到桌面。