最近公司让写一个可以给对应的APP创建对应的快捷方式的需求,网上搜了很多都是12年左右的,但是好像都不能用,下面有一篇,自测可以用,希望可以帮助到大家
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
ShortcutManager shortcutManager = (ShortcutManager)context.getSystemService(Context.SHORTCUT_SERVICE);
if (shortcutManager == null) {
return;
}
if (shortcutManager.isRequestPinShortcutSupported()) {
ComponentName comp = new ComponentName(“应用包名”, “应用名”);
Intent shortcutInfoIntent = new Intent(context, MainActivity.class);
shortcutInfoIntent.setAction(Intent.ACTION_VIEW);
shortcutInfoIntent.setComponent(comp);
shortcutInfoIntent.setData(Uri.parse(“包名”));
ShortcutInfo info = new ShortcutInfo.Builder(context,s)
.setIcon(Icon.createWithResource(context,“图片id”)) //我自己的是int类型大家可以看源码改bitmap类型等
.setShortLabel("快捷方式名")
.setIntent(shortcutInfoIntent)
.build();
PendingIntent shortcutCallbackIntent = PendingIntent.getBroadcast(context, 0, shortcutInfoIntent, PendingIntent.FLAG_UPDATE_CURRENT);
shortcutManager.requestPinShortcut(info, shortcutCallbackIntent.getIntentSender());
}
}