【问题标题】:Create URL shortcut programmatically in Android在 Android 中以编程方式创建 URL 快捷方式
【发布时间】:2023-04-02 22:11:01
【问题描述】:

我正在尝试找出如何以编程方式在 Android 主屏幕中创建 URL 快捷方式,但找不到答案。在 StackOverflow 上被问过一次,但唯一的答案与问题无关,排名为 -2。有人可以帮忙吗?

提前致谢。

【问题讨论】:

    标签: android shortcut


    【解决方案1】:
    private void setShortcut(String url) {
            String query = Uri.encode(url, "UTF-8");
            browserIntent = new Intent(CATEGORY_BROWSABLE, Uri.parse(Uri.decode(query)));
            browserIntent.setAction(ACTION_VIEW);
    
            if (ShortcutManagerCompat.isRequestPinShortcutSupported(getApplicationContext())) {
                ShortcutInfoCompat shortcutInfo = new ShortcutInfoCompat.Builder(getApplicationContext(), "1")
                        .setIntent(browserIntent) // !!! intent's action must be set on oreo
                        .setShortLabel("Test")
                        .setIcon(IconCompat.createWithResource(getApplicationContext(), R.drawable.bg))
                        .build();
                ShortcutManagerCompat.requestPinShortcut(getApplicationContext(), shortcutInfo, null);
            } else {
                Toast.makeText(getApplicationContext(),"launcher does not support short cut icon",Toast.LENGTH_LONG).show();
            }
        }
    

    我已经在模拟器上对此进行了测试,它可以工作并在浏览器中打开 url,您可以使用 .setIntent(browserIntent) 方法将意图设置为快捷方式,并在我创建的 browserIntent 代码中在浏览器中打开 url

    【讨论】:

    • 感谢您的回答。我正在开发自定义启动器,我得到 ShortcutManagerCompat.isRequestPinShortcutSupported(getApplicationContext()) 为我的启动器返回 false。您是否知道我应该为我的启动器申请哪些更改才能使其支持请求 pin 快捷方式?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-23
    • 2015-10-30
    • 1970-01-01
    • 2015-10-04
    相关资源
    最近更新 更多