【问题标题】:Trying to pass the URL from webview to browser试图将 URL 从 webview 传递到浏览器
【发布时间】:2011-12-27 10:35:16
【问题描述】:

我正在使用高度为 60dp 的 webview,并且我将本地 html 文件传递​​给它,默认情况下,当我单击 webview 中的链接时,它必须打开浏览器。但奇怪的是它在 webview 中打开了链接,我也尝试了 webview 客户端并尝试通过意图将响应 url 传递给默认浏览器,但徒劳无功..

我的代码 sn-p:

    WebViewClient yourWebClient = new WebViewClient()
       {

           @Override
           public boolean shouldOverrideUrlLoading(WebView  view, String  url)
           {
               System.out.println("Inside WebViewClient the URL is....."+url);

               Intent i = new Intent(Intent.ACTION_VIEW);
               i.setData(Uri.parse(url));
               startActivity(i);

            return true;
           }
       };

    WebView ad = (WebView) findViewById(R.id.webview1);
    ad.getSettings().setJavaScriptEnabled(true);
    ad.loadUrl(feed.getItem(position).getLink());
    ad.getSettings().setLoadWithOverviewMode(true);
    ad.getSettings().setUseWideViewPort(true);
    ad.setInitialScale(100);
        ad.setWebViewClient(yourWebClient);
    ad.loadUrl("file:///android_asset/advertisement.htm");

【问题讨论】:

    标签: android webview webchromeclient


    【解决方案1】:

    停止当前的 webview 加载。由于广告是 WebView 的对象,请尝试这样做:

      @Override
           public boolean shouldOverrideUrlLoading(WebView  view, String  url)
           {
               System.out.println("Inside WebViewClient the URL is....."+url);
    
                ad.stopLoading();
                Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                startActivity(intent);
    
            return true;
           }
    

    在为您的 WebView 添加设置时也添加这个:

     ad.getSettings().setSupportMultipleWindows(false);
    

    编辑:

    尝试将 chrome 客户端与您的 webview 关联,然后检查:

     ad.setWebChromeClient(new MyWebChromeClient());
    

    希望这对你有用。

    【讨论】:

    • 感谢 Usama Sarwar 的回答,但不幸的是它并没有解决我的问题。如果您看到我正在加载的 ads.html 文件有一个来自 google 的 java 脚本代码,它会生成带有相应广告链接的广告。您的代码和我的代码在尝试从 webview 打开超链接时将起作用,但在任何一种情况下都不会打印“WebViewClient”中的 sysout。
    • 感谢您的回复,现在我正在使用不同的 html 文件,我的问题已解决。
    猜你喜欢
    • 1970-01-01
    • 2014-07-24
    • 2011-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多