本文介绍了WebView + WebChromeClient 方法 onCreateWindow 未调用目标 =“_blank";的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Android API 级别 10 (sdk 2.3.3) 开发带有 WebViewcustom browser,不幸的是我没有不知道如何拦截来自 webpage 在新浏览器窗口中打开 URL 的请求,比如点击带有 target="_blank" 的链接.

I'm trying to develop a custom browser with WebView using Android API level 10 (sdk 2.3.3), unfortunately I don't know how to intercept request from the webpageto open URL in new browser window, like taps on links with target="_blank".

根据API Doc,我覆盖了WebChromeClientonCreateWindow,但在点击此类链接时不会调用.可能是这个 API 级别的错误?我还覆盖了 shouldOverrideUrlLoading 以避免 WebView 在内置浏览器中打开后续链接.

In conformity with the API Doc I have overridden the onCreateWindow of WebChromeClient, but is not called when such a link is tapped. Could be a bug of this API level? I'm also overriding shouldOverrideUrlLoading to avoid WebView opening subsequent links in the built-in browser.

这是我打开 google.com 的示例代码.要测试它,请点击新闻",然后点击任何新闻标题.Android 内置浏览器通常会在新的浏览器窗口中打开它.

Here is my sample code that opens google.com. To test it, tap "News" and then tap on any news title. The Android built-in browser normally opens it in a new browser window.

package com.myexample;

import android.app.Activity;
import android.os.Bundle;
import android.os.Message;
import android.util.Log;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

public class HelloAndroidActivity extends Activity {

  private static String TAG = "WebViewTest";
  private WebView mWebView;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.i(TAG, "onCreate");
    setContentView(R.layout.main);

    mWebView = (WebView) findViewById(R.id.mywebview);

    mWebView.setWebViewClient(new WebViewClient() {
      @Override
      public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return true;
      }
    });

    mWebView.setWebChromeClient(new WebChromeClient() {
      @Override
      public boolean onCreateWindow(WebView view, boolean dialog, boolean userGesture, Message resultMsg) {
        Toast.makeText(getApplicationContext(), "OnCreateWindow", Toast.LENGTH_LONG).show();
        return true;
      }
    });
    mWebView.loadUrl("http://www.google.com");
  }

}

推荐答案

确保将 supportMultipeWindows 设置为 true.没有它,WebChromeClient 的 onCreateWindow 将永远不会被调用.

Make sure you set supportMultipeWindows to true. Without it the onCreateWindow of the WebChromeClient will never get called.

WebSettings settings = webView.getSettings();
settings.setSupportMultipleWindows(true);

然后注册一个 WebChromeClient 并覆盖 onCreateWindow

Then register a WebChromeClient and override onCreateWindow

 webView.setWebChromeClient(new WebChromeClient() {
        @Override public boolean onCreateWindow(WebView view, boolean dialog, boolean userGesture, Message resultMsg)
        {
            WebView newWebView = new WebView(getContext());
            addView(newWebView);
            WebView.WebViewTransport transport = (WebView.WebViewTransport) resultMsg.obj;
            transport.setWebView(newWebView);
            resultMsg.sendToTarget();
            return true;
        }
    });

这篇关于WebView + WebChromeClient 方法 onCreateWindow 未调用目标 =“_blank";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-29 21:34