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第十五次面试总结(第三方组件和adb命令)
android
追随远方1 小时前
Android音频开发:Speex固定帧与变长帧编解码深度解析
android·音视频
消失的旧时光-19431 小时前
Android和硬件通信
android
0wioiw01 小时前
安卓基础(编译.Class)
android
0wioiw01 小时前
安卓基础(aar)
android
_一条咸鱼_1 小时前
Android Runtime链接(Linking)阶段准备工作(27)
android·面试·android jetpack
aqi002 小时前
FFmpeg开发笔记(六十四)使用国产的RedPlayer播放器观看网络视频
android·ffmpeg·音视频·直播·流媒体
雨白2 小时前
扩展函数和运算符重载
android
casual_clover4 小时前
Android Studio 解决首次安装时下载 Gradle 慢问题
android·ide·android studio
天天爱吃肉82184 小时前
新能源汽车热管理核心技术解析:冬季续航提升40%的行业方案
android·python·嵌入式硬件·汽车