WebView爬坑之旅2--a標籤是target="_blank"時建立新視窗
阿新 • • 發佈:2019-01-01
如果網頁裡連結的a標籤是target=”_blank”,而碰巧你的WebView又沒有處理,那這個時候就會出現狂點連結/button頁面卻無動於衷的情況,給人想砸手機的感覺…
好了,解決方案其實也挺簡單的,繼承WebChromeClient,重寫onCreateWindow是關鍵,請看程式碼:
/**
* 當網頁裡a標籤target="_blank",開啟新視窗時,這裡會呼叫
*/
@Override
public boolean onCreateWindow(WebView webView, boolean isDialog, boolean isUserGesture, Message resultMsg) {
X5WebView newWebView = new X5WebView(activity);
X5WebView.WebViewTransport transport = (WebView.WebViewTransport) resultMsg.obj;
newWebView.setWebChromeClient(new CustomWebChromeClient(activity));
newWebView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading (WebView view, String url) {
if (activity != null) {
//攔截url,跳轉新視窗=,=
Intent intent = new Intent(activity, CommWebActivity.class);
intent.putExtra(Constants.INTENT_KEY_URL, url);
activity.startActivity(intent);
}
//防止觸發現有介面的WebChromeClient的相關回調
return true;
}
});
transport.setWebView(newWebView);
resultMsg.sendToTarget();
return true;
// return super.onCreateWindow(webView, isDialog, isUserGesture, message);
}
前提要設定這個,不然不會走回調
webSetting.setSupportMultipleWindows(true);