Android 在WebViewClient.shouldOverrideUrlLoading之前调用window.beforeunload
在配置了WebView和WebViewClient的Android应用程序中,从js调用后,在调用Android之前会触发js事件。这是一种正当的、有意的行为吗?是否有方法在没有窗口的情况下拦截打开的url。在触发卸载事件之前?请参阅此示例代码,可能有助于您:Android 在WebViewClient.shouldOverrideUrlLoading之前调用window.beforeunload,android,webview,android-webview,android-webview-javascript,Android,Webview,Android Webview,Android Webview Javascript,在配置了WebView和WebViewClient的Android应用程序中,从js调用后,在调用Android之前会触发js事件。这是一种正当的、有意的行为吗?是否有方法在没有窗口的情况下拦截打开的url。在触发卸载事件之前?请参阅此示例代码,可能有助于您: final WebView webView = (WebView)findViewById(R.id.webview); // Enable javascript webView.getSettings().se
final WebView webView = (WebView)findViewById(R.id.webview);
// Enable javascript
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient() {
@SuppressWarnings("deprecation")
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
final Uri uri = Uri.parse(url);
return customUriHanlder(uri);
}
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
final Uri uri = request.getUrl();
return customUriHanlder(uri);
}
private boolean customUriHanlder(final Uri uri) {
Log.i(TAG, "Uri =" + uri);
final String host = uri.getHost();
final String scheme = uri.getScheme();
// you can set your specific condition
if (true) {
// Returning false means that you are going to load this url in the webView itself
return false;
} else {
// Returning true means that you need to handle what to do with the url
// open web page in a Browser
final Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
return true;
}
}
});
// Load the webpage
webView.loadUrl("https://google.com/");
使用customurihandler
功能,您可以在vebview本身或浏览器中加载url。在google文档中,如果提供了WebViewClient,返回
true
会导致当前WebView中止加载URL,而返回false
会导致WebView像往常一样继续加载URL 要停止窗口。在卸载之前,请执行您尝试的操作???@chiragsoni我假设使用WebViewClient.shouldOverrideUrlLoading将取消与窗口相关的任何其他js事件。open,我错了吗?我不确定这一点。