Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
802 views
in Technique[技术] by (71.8m points)

android - WebView + WebChromeClient method onCreateWindow not called for 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".

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.

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");
  }

}

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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);

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...