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

相关推荐
00后程序员张5 分钟前
Windows / Linux / Mac 上不用 Xcode 把 IPA 上传到 App Store,upload 命令详解
android·ios·小程序·https·uni-app·iphone·webview
秦少游在淮海23 分钟前
id - Android、iOS
android·ios
恋猫de小郭32 分钟前
超好用 R8 Configuration Analyzer, 优化你的 App 大小和内存
android·前端·flutter
又见情义39 分钟前
Android 13 系统应用裁剪:RK3568平台下的精简之道
android
苦瓜花1 小时前
【Android】LiveData
android
格林威1 小时前
C#图像处理:使用imagemagick实现像素放大水印转换等多种功能
android·开发语言·图像处理·人工智能·计算机视觉·c#·视觉检测
雨白8 小时前
深入理解 Java/Kotlin 反射、注解与泛型:从原理到实战
android·架构
喜欢打篮球的普通人13 小时前
LLVM Backend Lowering 从入门到实战:把 IR 变成机器码的完整链路
android·java·数据库
萝卜er16 小时前
BroadcastReceiver 静态注册与动态注册-《Android深水区(七)》
android
萝卜er16 小时前
ContentProvider 与跨进程数据访问-《Android深水区(八)》
android