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

相关推荐
用户833525025378512 小时前
ViewModel详细解析
android
问心无愧051313 小时前
ctf show web入门91
android·前端·笔记
YF021113 小时前
Android App 高效升级指南:OkDownload 多线程断点续传与全版本安装适配
android·okhttp·app
huangliang070313 小时前
MySQL 中的 distinct 和 group by 哪个效率更高?
android·数据库·mysql
程思扬13 小时前
Android 悬浮窗状态错乱终极解决方案:告别 onResume
android·网络
逸Y 仙X13 小时前
文章二十九:ElasticSearch分桶聚合
android·大数据·elasticsearch·搜索引擎·全文检索
陆业聪13 小时前
网络监控与容灾:让网络问题无处遁形
android·性能优化·启动优化
问心无愧051313 小时前
ctf show web入门 89
android·前端·笔记
高旭的旭14 小时前
Android Perfetto Profilers Skills 简明使用指南
android
alexhilton1 天前
Android上的ZeroMQ:用发布/订阅模式连接Linux服务
android·kotlin·android jetpack