use of com.tencent.smtt.export.external.interfaces.JsResult in project Ency by xiarunhao123.
the class WebActivity method initWebView.
@SuppressLint("SetJavaScriptEnabled")
private void initWebView() {
WebSettings settings = webView.getSettings();
settings.setCacheMode(WebSettings.LOAD_NO_CACHE);
settings.setJavaScriptCanOpenWindowsAutomatically(true);
settings.setJavaScriptEnabled(true);
settings.setDomStorageEnabled(true);
settings.setDatabaseEnabled(true);
settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NORMAL);
String dir = this.getApplicationContext().getDir("database", Context.MODE_PRIVATE).getPath();
settings.setGeolocationEnabled(true);
settings.setGeolocationDatabasePath(dir);
if (sharePrefManager.getNightMode()) {
webView.setDayOrNight(false);
} else {
webView.setDayOrNight(true);
}
webView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView webView, String s) {
return super.shouldOverrideUrlLoading(webView, s);
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
swipeRefreshLayout.setRefreshing(true);
super.onPageStarted(view, url, favicon);
}
@Override
public void onPageFinished(WebView view, String url) {
swipeRefreshLayout.setRefreshing(false);
super.onPageFinished(view, url);
}
@Override
public void onReceivedSslError(WebView webView, SslErrorHandler sslErrorHandler, com.tencent.smtt.export.external.interfaces.SslError sslError) {
sslErrorHandler.proceed();
}
});
webView.setWebChromeClient(new WebChromeClient() {
// 扩展支持alert事件
@Override
public boolean onJsAlert(WebView webView, String url, String message, final JsResult jsResult) {
new MaterialDialog.Builder(mContext).title("提示").content(message).positiveText(R.string.yes).onPositive(new MaterialDialog.SingleButtonCallback() {
@Override
public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
jsResult.confirm();
}
}).cancelable(false).build().show();
return true;
}
@Override
public void onGeolocationPermissionsHidePrompt() {
super.onGeolocationPermissionsHidePrompt();
}
@Override
public void onGeolocationPermissionsShowPrompt(final String origin, final GeolocationPermissionsCallback callback) {
final boolean remember = false;
new MaterialDialog.Builder(mContext).title("地理位置授权").content("允许" + origin + "获取您当前的地理位置信息吗").positiveText("允许").onPositive(new MaterialDialog.SingleButtonCallback() {
@Override
public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
callback.invoke(origin, true, remember);
}
}).negativeText("拒绝").negativeColorRes(R.color.black).onNegative(new MaterialDialog.SingleButtonCallback() {
@Override
public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
callback.invoke(origin, false, remember);
}
}).cancelable(false).build().show();
}
});
webView.setOnScrollListener(new X5WebView.IScrollListener() {
@Override
public void onScrollChanged(int scrollY) {
// webView在顶部
if (scrollY == 0) {
swipeRefreshLayout.setEnabled(true);
} else // webView不是顶部
{
swipeRefreshLayout.setEnabled(false);
}
}
});
// 点击返回
webView.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && webView.canGoBack()) {
webView.goBack();
return true;
}
return false;
}
});
webView.loadUrl(url);
}
Aggregations