WebView使用Intent.ACTION_VIEW处理重定向点击链接 [英] WebView handle redirect click link with Intent.ACTION_VIEW

查看:105
本文介绍了WebView使用Intent.ACTION_VIEW处理重定向点击链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用不同设备上的 Intent.ACTION_VIEW 单击 webview 内容时,我很难管理打开链接.

I've a difficult managing open link on webview content click with Intent.ACTION_VIEW on different device.

我的情况是:我的应用程序内的 webview 中显示了一些html代码,此内容具有一些链接(href),然后在单击事件时我想打开与外部应用程序的链接而不是在同一Web视图中.

My scenario is: i've some html code displayed within a webview inside my app, this content have some link (href), then on click event i want to open the link with external app and not inside the same web view.

我通过以下方式实现了这一目标:

I've achieved this by:

webview.setWebViewClient(new WebViewClient() {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(request.getUrl().toString()));
        activity.startActivity(intent);    
        return true;
    }
});

因此,问题在于这不会在所有设备上发生.

So, the problem is that this doesn't happen an all devices.

使用Android 7可以正常运行,但在版本6上则无法运行.无论如何管理不同的版本,我已经使用了不同的实现.

With Android 7 it's working but on version 6 doesn't. Anyway to manage different version i've already used different implementation.

webview.setWebViewClient(new WebViewClient() {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
        activity.startActivity(intent);
        return true;
    }
});

我已经看过stackoverflow了,但是我发现的唯一答案是使用上面的代码.

I've already look around on stackoverflow but the only answer i've found it's to use the code above.

感谢支持

更新-18/02/15 我已经解决了这个问题,将两个 @Override 方法放在一起,如下所示:

UPDATE - 15/02/18 I've solved the problem, putting the two @Override method together like this:

webview.setWebViewClient(new WebViewClient() {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(request.getUrl().toString()));
        activity.startActivity(intent);    
        return true;
    }

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
        activity.startActivity(intent);
        return true;
    }
});

推荐答案

这是因为您使用的方法仅在API 24(7.0)中添加,因此在较旧的API中不存在,请检查此处.您需要做的是使用旧版本.它已声明为 Deprecated ,但您仍然可以使用它.

That is because the method you are using was added only in API 24 (7.0), so it doesn't exist on older APIs, check here. What you need to do is to use the old one in this case. It is declared as Deprecated, but you still can use it.

这篇关于WebView使用Intent.ACTION_VIEW处理重定向点击链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆