美文网首页
Android添加桌面快捷方式,兼容所有版本

Android添加桌面快捷方式,兼容所有版本

作者: 进击的小大叔 | 来源:发表于2020-01-02 09:26 被阅读0次

由于Android版本碎片导致各种兼容问题,遂直接使用Support包下兼容管理类ShortcutManagercomat即可

首先确保获取全权限
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
8.0以下系统可能出现应用未安装的问题,在AndroidManifest文件里快捷方式启动的页面acitivity标签里添加android:exported="true"
public static void addShortCutCompact(Context context) {
    if (ShortcutManagerCompat.isRequestPinShortcutSupported(context)) {
        Intent shortcutInfoIntent = new Intent(context, ShortcutActivity.class);
        shortcutInfoIntent.setAction(Intent.ACTION_VIEW); //action必须设置,不然报错
        ShortcutInfoCompat info = new ShortcutInfoCompat.Builder(context, "id")
                .setIcon(IconCompat.createWithResource(context, R.mipmap.ic_launcher))
                .setShortLabel("name")
                .setIntent(shortcutInfoIntent)
                .build();
        //当添加快捷方式的确认弹框弹出来时,将被回调,自定义Receiver
        PendingIntent shortcutCallbackIntent = PendingIntent.getBroadcast(context, 0, new Intent(context, ShortcutReceiver.class),PendingIntent.FLAG_UPDATE_CURRENT);
        ShortcutManagerCompat.requestPinShortcut(context, info,     
        shortcutCallbackIntent.getIntentSender());
    }
}

相关文章

网友评论

      本文标题:Android添加桌面快捷方式,兼容所有版本

      本文链接:https://www.haomeiwen.com/subject/abngoctx.html