一、概述
1、Shortcuts是android7.0的新特性,能配置shortcuts的activity必须符合action是android.intent.action.MAIN且category是android.intent.category.LAUNCHER,;Shortcuts实现方式分为静态和动态注册。
2、vivo桌面v9.3.0以后的版本已经开放三方应用自定义快捷方式能力。
二、接入后效果
1、接入后展示效果
自定义功能由应用自身定义,最多显示4个,如上图中“微博”自定义的“热门微博”、“扫一扫”、“发微博”快捷功能。
系统提供默认的功能,由系统确定,不支持自定义,如上图中系统默认的“卸载web生成app”、“应用信息”、“编辑桌面”功能。
2、具体实现
参数说明:
shortcutId:快捷方式的唯一标示,必传属性
shortcutLongLabel:桌面优先显示此字段配置的名字
shortcutShortLabel:没有shortcutLongLabel字段或者为空时展示此字段
shortcutDisabledMessage:禁止使用快捷方源码生成app软件式的显示字段
icon:桌面显示快捷方式需要的图标
action:通常是android.intent.action.VIEW,也可以根据自己需要更改
targetClass:对应跳转activity的全路径名
targetPackage:对应跳转activity的包名
静态注册方式:
在res文件夹下创建xml文件夹,其中放入shortcuts.xml文件,同时在AndroidManifest.xml中action符合上述2个要求的activity标签中添加<meta-data android:name=”android.app.shortcuts” android:resource=”@xml/shortcuts”/>即可,具体shortcuts.xml和AndroidManifest中的配置如下图:
特别注意其中的shortcutLongLabel,shortcutShortLabel,shortcutDisabledMessage中的string必须写在string.xml中,不然无编译




动态注册方式:
动态注册相对于静态注册灵活性更高,只要是在符合上述2个条件的Activity中可以根据需求进行添加和移除,此添加和移除原生代码是在主线程中执行,实测可以放到子线程,一般正常添加快捷方式都是在onCreate()中第一次进入直接添加即可,重复添加和多次删除同一个ID,测试无异常;
写法如下图:




编辑:yimen,如若转载,请注明出处:https://www.yimenapp.com/kb-yimen/11640/
部分内容来自网络投稿,如有侵权联系立删