当我点击带有 , 的超链接时target="_blank"
attributes
,WebChromeClient#onCreateWindow
会调用但我现在找不到新窗口将打开什么网址的方法?主机页面 url 是我唯一能知道的吗?
我想根据目标 url 更改应用程序行为。任何帮助表示赞赏,谢谢!
当我点击带有 , 的超链接时target="_blank"
attributes
,WebChromeClient#onCreateWindow
会调用但我现在找不到新窗口将打开什么网址的方法?主机页面 url 是我唯一能知道的吗?
我想根据目标 url 更改应用程序行为。任何帮助表示赞赏,谢谢!
我可以通过如下调用来获得点击的网址
public boolean onCreateWindow(WebView view, boolean isDialog, boolean isUserGesture, Message resultMsg) {
WebView.HitTestResult result = view.getHitTestResult();
int type = result.getType();
String data = result.getExtra();
// do something
}
也许我迟到了,但上述解决方案都不适用于 Android 10。所以我是这样做的:
@Override
public boolean onCreateWindow(WebView view, boolean dialog, boolean userGesture, android.os.Message resultMsg) {
WebView newWebView = new WebView(view.getContext());
WebView.WebViewTransport transport = (WebView.WebViewTransport) resultMsg.obj;
transport.setWebView(newWebView);
resultMsg.sendToTarget();
newWebView.setWebViewClient(new WebViewClient() {
@SuppressLint("NewApi")
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
return shouldOverrideUrlLoading(view, request.getUrl().toString());
}
@Override
public boolean shouldOverrideUrlLoading(WebView View, String url) {
if (url != null && !url.isEmpty()) {
// HERE IS URL WHICH YOU WANT
}
return false;
}
});
return true;
}
我使用以下代码:
public boolean onCreateWindow(WebView view, boolean isDialog, boolean isUserGesture, Message resultMsg) {
Message href = view.getHandler().obtainMessage();
view.requestFocusNodeHref(href);
var url = href.getData()?.getString("url");
// rest of your code
}
以下片段将完成工作。
@Override
public boolean onCreateWindow (WebView view,boolean isDialog, boolean isUserGesture, Message resultMsg){
//The default implementation
//return super.onCreateWindow(view, isDialog, isUserGesture, resultMsg);
//This way you can get the URL
String url = null;
if (webView.getHitTestResult().getType() == WebView.HitTestResult.SRC_ANCHOR_TYPE
|| webView.getHitTestResult().getType() == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE) {
url = webView.getHitTestResult().getExtra();
}
return true;
}