【问题标题】:open ads in external browser in webview android在 webview android 的外部浏览器中打开广告
【发布时间】:2013-02-26 06:22:35
【问题描述】:

我用 webview 创建了应用程序,我想在 webview 中加载所有内部链接并在 android 浏览器中加载外部链接。现在的问题是我正在使用 html 广告,当我点击广告时,我想打开外部浏览器,但它在 webview 中打开。唯一的问题是广告,否则一切正常。那我该怎么做呢?

我的代码如下:

`class MyWebViewClient 扩展 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在做什么?
  • 我使用带有 javascript.(document.write) 的简单 3d 派对横幅广告。

标签: webview


【解决方案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

【讨论】:

    【解决方案2】:

    我做了一些修改,它非常适合横幅广告。 我做了以下更改:

    • 如果条件改变了。
    • 如文档所述,我正在为“if”块返回 false:

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

    @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;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-12-09
      • 2021-06-12
      • 2014-07-12
      • 2018-02-11
      • 2017-08-06
      • 1970-01-01
      • 2012-09-24
      相关资源
      最近更新 更多