用场景
autojs创建桌面快捷方式, 可配合intent使用
效果展示
原理
shortcutManager.requestPinShortcut
安卓官方文档
https://developer.android.google.cn/reference/kotlin/android/content/pm/ShortcutManager?hl=en
知识点
- 判断安卓版本
- 启动器是否支持创建快捷方式
- 创建
intent
- 获取
app
图标 - 实例化
ShortcutInfo
- 请求创建快捷方式
代码讲解
- 判断安卓版本
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) { log("本代码仅在华为安卓8手机上测试通过"); } else { log("需要安卓8(包含)以上"); }
- 启动器是否支持创建快捷方式
shortcutManager = context.getSystemService(context.SHORTCUT_SERVICE); requestPinShortcutSupported = shortcutManager.isRequestPinShortcutSupported(); log(TAG, "启动器是否支持固定快捷方式: " + requestPinShortcutSupported);
- 创建
intent
var shortcutInfoIntent = new Intent(); shortcutInfoIntent.setAction("android.settings.ACCESSIBILITY_SETTINGS");
- 获取
app
图标
name = "设置"; icon_path = "/sdcard/" + name + ".jpg"; icon = getIcon(icon_path);
- 实例化
ShortcutInfo
let info = new ShortcutInfo.Builder(context, randomNum) .setIcon(icon) .setShortLabel("短名" + randomNum) .setLongLabel("长名" + randomNum) .setIntent(shortcutInfoIntent) .build();
- 请求创建快捷方式
shortcutManager.requestPinShortcut(info, shortcutCallbackIntent.getIntentSender());