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.标题太长,添加之前需要编辑一下。

相关推荐
*星星之火*3 小时前
【GPT入门】第5课 思维链的提出与案例
android·gpt
EasyCVR3 小时前
EasyRTC嵌入式视频通话SDK的跨平台适配,构建web浏览器、Linux、ARM、安卓等终端的低延迟音视频通信
android·arm开发·网络协议·tcp/ip·音视频·webrtc
韩家老大4 小时前
RK Android14 在计算器内输入特定字符跳转到其他应用
android
张拭心6 小时前
2024 总结,我的停滞与觉醒
android·前端
夜晚中的人海6 小时前
【C语言】------ 实现扫雷游戏
android·c语言·游戏
ljx14000525507 小时前
Android AudioFlinger(一)——初识AndroidAudio Flinger
android
ljx14000525507 小时前
Android AudioFlinger(四)—— 揭开PlaybackThread面纱
android
Codingwiz_Joy7 小时前
Day04 模拟原生开发app过程 Androidstudio+逍遥模拟器
android·安全·web安全·安全性测试
叶羽西7 小时前
Android15 Camera框架中的StatusTracker
android·camera框架
梦中千秋8 小时前
安卓设备root检测与隐藏手段
android