Android启动意图通过Webview获取(将字符串转换为意图)
创始人
2024-10-09 16:01:21
0

要实现在Webview中通过字符串启动Intent,需要通过WebChromeClient的onCreateWindow方法来实现。以下是一个示例代码:

webView.setWebChromeClient(new WebChromeClient() {
    @Override
    public boolean onCreateWindow(WebView view, boolean isDialog, boolean isUserGesture, Message resultMsg) {
        WebView newWebView = new WebView(context);
        newWebView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                Intent intent = null;
                try {
                    intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME);
                } catch (URISyntaxException e) {
                    e.printStackTrace();
                }
                if (intent != null) {
                    try {
                        context.startActivity(intent);
                    } catch (ActivityNotFoundException e) {
                        e.printStackTrace();
                    }
                    return true;
                }
                return false;
            }
        });
        WebView.WebViewTransport transport = (WebView.WebViewTransport) resultMsg.obj;
        transport.setWebView(newWebView);
        resultMsg.sendToTarget();
        return true;
    }
});

此代码创建了一个新的Webview来处理应用程序外的连接。在shouldOverrideUrlLoading方法中,检查URL中是否包含URI Intent Scheme,如果是则将其解析成一个Intent对象并启动Activity。应该注意的是,此方法只执行在应用程序中没有Activity可以处理的情况下。

相关内容

热门资讯

解谜透视!wejoker内置辅... 解谜透视!wejoker内置辅助,微乐自建房怎么提高运气,法子教程(有挂透明挂)-哔哩哔哩1、wej...
解迷透视!wepoker作弊方... 解迷透视!wepoker作弊方法,微乐a3纸牌有脚本,大纲教程(真的有挂)-哔哩哔哩1)wepoke...
辅助透视!wpk透视是真的假的... 辅助透视!wpk透视是真的假的,微乐自建房脚本最新版本更新内容,步骤教程(有挂秘籍)-哔哩哔哩暗藏猫...
专业透视!智星菠萝有挂吗,微乐... 专业透视!智星菠萝有挂吗,微乐自建房脚本下载,模块教程(了解有挂)-哔哩哔哩1、下载好智星菠萝有挂吗...
有挂透视!aapoker透视怎... 有挂透视!aapoker透视怎么用,微乐自建房运势高有什么表现,机巧教程(有挂技术)-哔哩哔哩1、a...
解密透视!德普之星透视辅助,小... 解密透视!德普之星透视辅助,小程序微乐陕西挖坑辅助器,方式教程(有挂透明挂)-哔哩哔哩1、打开软件启...
揭露透视!wejoker辅助器... 揭露透视!wejoker辅助器怎么卖,微信小程序微乐辅助器免费下载,妙招教程(有挂辅助)-哔哩哔哩1...
普及透视!拱趴大菠萝挂哪里,微... 普及透视!拱趴大菠萝挂哪里,微乐小程序授权,要领教程(有挂助手)-哔哩哔哩拱趴大菠萝挂哪里脚本下载中...
普及透视!德州透视脚本,微信小... 普及透视!德州透视脚本,微信小程序游戏辅助器,学习教程(有挂分享)-哔哩哔哩1、全新机制【德州透视脚...
详细透视!hhpoker辅助软... 详细透视!hhpoker辅助软件是真的么,微乐小程序挂哪里,经验教程(有挂解密)-哔哩哔哩该软件可以...