Android创建快捷方式到桌面

效果图

参考

https://blog.51cto.com/u_16175498/8811197https://blog.51cto.com/u_16175498/8811197

权限

复制代码
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />

实现

java 复制代码
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    ShortcutManager shortcutManager = getSystemService(ShortcutManager.class);
    if (shortcutManager.isRequestPinShortcutSupported()) {
        String title = list_webView.get(currentPage).getTitle();
        String url = list_webView.get(currentPage).getUrl();
        Bitmap bitmap = list_webView.get(currentPage).getFavicon();
        if (bitmap == null)
            bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
        Icon icon = Icon.createWithBitmap(bitmap);
        Intent shortcutIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
        ShortcutInfo shortcutInfo = new ShortcutInfo.Builder(getApplicationContext(), url)
            .setShortLabel(title)
            .setLongLabel(title)
            .setIcon(icon)
            .setIntent(shortcutIntent)
            .build();
        Intent pinnedShortcutCallbackIntent = shortcutManager.createShortcutResultIntent(shortcutInfo);
        PendingIntent successCallback = PendingIntent.getBroadcast(getApplicationContext(), 0, pinnedShortcutCallbackIntent, 0);
        shortcutManager.requestPinShortcut(shortcutInfo, successCallback.getIntentSender());
    }
} else {
    Toast.makeText(getApplicationContext(), "不支持创建快捷方式", Toast.LENGTH_SHORT).show();
}

注意

1.Android O 以上才支持。

2.ShortcutInfo.Builder(getApplicationContext(), id),id不一样才会添加,我直接填网址了。

3.标题太长,添加之前需要编辑一下。

相关推荐
沐怡旸2 小时前
【底层机制】Ashmem匿名共享内存:原理与应用深度解析
android·面试
用户2018792831673 小时前
Activity结束动画与System.exit(0)的黑屏之谜
android
Proud lion4 小时前
Apipost 脚本高频场景最佳实践:搞定接口签名验证、登录令牌刷新、动态参数生成等
android
介一安全4 小时前
【Frida Android】实战篇5:SSL Pinning 证书绑定绕过 Hook 教程(二)
android·网络安全·逆向·安全性测试·frida
2501_937193144 小时前
PLB-TV 影视!无广告 + 4K 高清
android·源码·源代码管理·机顶盒
阿斌_bingyu7095 小时前
uniapp实现android/IOS消息推送
android·ios·uni-app
Android系统攻城狮5 小时前
Android内核进阶之周期更新PCM状态snd_pcm_period_elapsed:用法实例(九十二)
android·pcm·android内核·音频进阶
Cola可洛5 小时前
修复Flyme移植BUG
android·bug
消失的旧时光-19436 小时前
Kotlinx.serialization 使用指南
android·kotlin·json
消失的旧时光-19437 小时前
Kotlinx.serialization 项目集成
android·kotlin·json