在Android的Webview中打开外部浏览器的广告。

3

我创建了一个带有Webview的应用程序,希望在Webview中加载所有内部链接,并在Android浏览器中加载外部链接。现在问题是当我使用html广告时,当我点击广告时,我想要在外部浏览器中打开,但它却在Webview中打开。除了广告之外,其他一切都正常工作。那么我该怎么做呢?

我的代码如下:

`class MyWebViewClient extends WebViewClient {

@Override   
public boolean shouldOverrideUrlLoading(WebView view, String url) { if (Uri.parse(url).getHost().equals("www.mysite.com")) {
    view.loadUrl(url);
    return true;
}else{

        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
        startActivity(intent);
        return true;}}`

你能展示一下广告链接的样子吗?如果有像onclick事件这样的js在执行什么操作吗? - Aloong
我使用简单的第三方横幅广告和JavaScript(document.write)。 - MacAnony
2个回答

1

我进行了一些修改,横幅广告完美工作。 我做出了以下更改:

  • 更改了if条件。
  • 像文档中所说的那样,对于 'if' 块,我返回 false:

如果提供了 WebViewClient,则返回 true 表示主机应用程序处理 url, 而返回 false 表示当前 WebView 处理 url

@Override   
public boolean shouldOverrideUrlLoading(WebView view, String url) 
{ 
    if (url.contains("www.mysite.com")) 
    {
       view.loadUrl(url);
       return false;
    }else
    {
       Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
       startActivity(intent);
       return true;
    }
}

1
你的代码应该是:

@Override   
public boolean shouldOverrideUrlLoading(WebView view, String url) {
    if (Uri.parse(url).getHost().equals("www.mysite.com")) {
        return true;
    }else{
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
        startActivity(intent);
        return false;
    }
}

我只做了以下更改:
1.) 返回true会在webview中加载URL,不需要使用view.loadUrl() 2.) 当你广播ACTION_VIEW意图时返回false。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接