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

相关推荐
前行的小黑炭1 小时前
Android Lifecycle代码分析:为什么使用;注解的方式为什么过期?源码分析;状态与事件
android
和煦的春风1 小时前
案例分析 | SurfaceFlinger 大片Runnable引起的卡顿
android·linux
浩宇软件开发2 小时前
Android开发,实现一个简约又好看的登录页
android·java·android studio·android开发
未扬帆的小船2 小时前
在gpt的帮助下安装chales的证书,用于https在root情况下抓包
android·charles
万户猴2 小时前
【 Android蓝牙-十】Android各版本蓝牙行为变化与兼容性指南
android·蓝牙
张风捷特烈4 小时前
FFmpeg 7.1.1 | 调试 ffmpeg.c 环境 - Widows&Clion&WSL
android·ffmpeg
努力努力再努力wz4 小时前
【Linux实践系列】:进程间通信:万字详解命名管道实现通信
android·linux·运维·服务器·c++·c
百锦再5 小时前
Android Studio 中使用 SQLite 数据库开发完整指南(Kotlin版本)
android·xml·学习·sqlite·kotlin·android studio·数据库开发
Gerry_Liang5 小时前
Java基础361问第16问——枚举为什么导致空指针?
android·java·开发语言
RichardLai886 小时前
[Flutter 基础] - Flutter基础组件 - Image
android·flutter