shortcuts实现

来源:互联网 发布:mac whirl亚洲人试色 编辑:程序博客网 时间:2024/06/05 16:37
1.使用条件
android 7.1以上
2.功能说明
长按桌面app图标出现快捷方式列表,点击可跳转到app的指定界面,用户也可以将其作为一个单独的快捷方式
3.效果截图


3.技术实现
分为两种方式,静态xml配置和动态代码配置,二者各有优劣,xml配置不需要运行app就有了,但是快捷方式不可以取消。动态代码配置更灵活,可以在任意界面新增或者取消快捷方式

静态xml实现方式:新建xml资源文件,将需要内置的快捷方式写入<shortcuts>标签内,其中包括快捷方式的图标、名称、跳转的url等

<?xml version="1.0" encoding="utf-8"?>
<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
<shortcut
 android:shortcutId="yydb"
 android:enabled="true"
 android:icon="@drawable/fanli"
 android:shortcutShortLabel="@string/yydb"
 android:shortcutLongLabel="@string/yydb"
 android:shortcutDisabledMessage="@string/yydb">
 <intent
    android:action="android.intent.action.VIEW"
    android:data="需要跳转的url"
    android:targetPackage="com.fanli.android.apps"
    android:targetClass="com.fanli.android.basicarc.ui.activity.CustomUrlBridgeActivity" />
    <categories android:name="android.shortcut.conversation"/>
 </shortcut>
 </shortcuts>
          
动态代码实现方式:需要先初始化shortcutinfo,给定快捷方式的属性,然后再调用setDynamicShortcuts告知系统改变快捷方式

ShortcutManager scManager = getSystemService(ShortcutManager.class);
ShortcutInfo scInfoTwo = new ShortcutInfo.Builder(SuperFanActivity.this, "superfan")
        .setLongLabel("超级返")
        .setShortLabel("超级返")
        .setIcon(Icon.createWithResource(SuperFanActivity.this, R.drawable.fanli))
        .setIntents(new Intent[]{new Intent("superFanActivity", Uri.EMPTY, SuperFanActivity.this,SuperFanActivity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK)}).build();
 scManager.setDynamicShortcuts(FanliApplication.shortCutInfos);
</pre>
取消快捷方式:根据快捷方式的id来进行取消,调用disableShortcuts方法实现,只有动态实现的可以取消

scManager.disableShortcuts(Arrays.asList(scInfoTwo.getId(),"该选项已取消"));
scManager.removeDynamicShortcuts(Arrays.asList(scInfoTwo.getId()));