android创建桌面快捷方式,适配8.0,避免重复创建

本文提供了一段用于在Android设备上创建桌面快捷方式的代码。该代码适用于不同版本的Android系统,包括Android O及以上版本和更早版本。通过使用此代码,开发者可以为应用程序创建指向特定URL的桌面快捷图标。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

直接上代码,可直接复制使用

    //创建桌面快捷方式
    private void createShortCut() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            String shortcutId = "com.itx.shortcut";
            boolean isExit = false;
            ShortcutManager shortcutManager = (ShortcutManager) getSystemService(Context.SHORTCUT_SERVICE);
            for (ShortcutInfo info : shortcutManager.getPinnedShortcuts()) {
                if (shortcutId.equals(info.getId())) {
                //判断快捷方式是否已存在
                    isExit = true;
                }
            }
            if (!isExit && shortcutManager.isRequestPinShortcutSupported()) {
                Uri content_url = Uri.parse("http://www.baidu.com/");
                Intent shortcutInfoIntent = new Intent(Intent.ACTION_VIEW, content_url);
                shortcutInfoIntent.setAction(Intent.ACTION_VIEW);
                ShortcutInfo info = new ShortcutInfo.Builder(this, shortcutId)
                        .setIcon(Icon.createWithResource(this, R.drawable.ibiguo_float))
                        .setShortLabel("快捷方式名字")
                        .setIntent(shortcutInfoIntent)
                        .build();
                shortcutManager.requestPinShortcut(info, null);
            }
        } else {
            Intent intentAddShortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
            intentAddShortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, "快捷方式名字");
            intentAddShortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
                    Intent.ShortcutIconResource.fromContext(this,
                            R.drawable.ibiguo_float));//设置Launcher的Uri数据
            intentAddShortcut.putExtra("duplicate", false);
            Intent intentLauncher = new Intent();
            Uri content_url = Uri.parse("http://www.baidu.com/");
            intentLauncher.setData(content_url);
            intentAddShortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intentLauncher);
            sendBroadcast(intentAddShortcut);
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大炮走火

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值