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

由于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());
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容