我正在尝试触发WebChromeClient.onRequestFocus()
回调。为了获取有关何时调用的信息,我深入研究了 android 项目测试。
我从 2009 年开始搜索WebChromeClient 的测试用例,并创建了以下代码来重现测试行为,这似乎不起作用。
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Activity activity = this;
final WebView webView = (WebView)findViewById(R.id.webView2);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
webView.getSettings().setSupportMultipleWindows(true);
webView.setWebChromeClient(new WebChromeClient() {
@Override
public void onCloseWindow(WebView window) {
super.onCloseWindow(window);
Log.i("", "================ onCloseWindow()===============================================================");
}
@Override
public boolean onCreateWindow(WebView view, boolean dialog, boolean userGesture,
Message resultMsg) {
WebView childView = new WebView(activity);
childView.setWebChromeClient(this);
WebView.WebViewTransport transport = (WebView.WebViewTransport) resultMsg.obj;
transport.setWebView(childView);
resultMsg.sendToTarget();
Log.i("", "================ onCreateWindow()===============================================================");
return true;
}
@Override
public void onRequestFocus(WebView view) {
Log.i("", "================ onRequestFocus()===============================================================");
}
});
webView.loadData("<html><body onLoad='"
+ "childWindow = window.open();"
+ "childWindow.document.title = \"javascript child window\";"
+ "childWindow.document.write(\"javascript child window\");"
+ "childWindow.focus();"
+ "setTimeout(function(){childWindow.close();}, 10000);"
+ "'><h1>Hello World!</h1></body></html>", "text/html", "UTF-8");
}
调试会话的 Logcat 确认调用onCreateWindow()
了onCloseWindow()
函数,但没有onRequestFocus()
调用。
有任何想法吗?
我使用 API 级别 19 (Android 4.4)