我有加载到Web视图中的动态URL。我已使用WebChromeClient处理Java脚本事件,因为我需要根据onJsAlert()中javascript中的事件来重定向用户。该网页是第一次加载。当我返回并加载相同的URL时,即加载它。但是,当我完成操作并收到javascript事件时,我正在开始一个新活动并完成webview活动。现在,当我加载另一个URL时,它不会加载。当我终止该应用程序并导航到webview活动时,它再次加载。
以下是我对网络视图的设置
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setDisplayZoomControls(false);
webView.getSettings().setDomStorageEnabled(true);
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);
webView.getSettings().setAppCacheEnabled(false);
webView.setWebViewClient(new myWebClient());
webView.setWebChromeClient(new MyJavaScriptChromeClient());
webView.loadUrl(signatureURL);
webView.setHorizontalScrollBarEnabled(false);
这是我用来显示进度对话框的WebViewClient
public class myWebClient extends WebViewClient {
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// TODO Auto-generated method stub
super.onPageStarted(view, url, favicon);
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (((BaseActivity) activity).checkConnection()) {
// TODO Auto-generated method stub
progressBar.setVisibility(View.VISIBLE);
view.loadUrl(url);
}
return true;
}
@Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
progressBar.setVisibility(View.GONE);
}
@Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
super.onReceivedError(view, request, error);
progressBar.setVisibility(View.GONE);
}
}
这就是我处理JS事件的方式
private class MyJavaScriptChromeClient extends WebChromeClient {
@Override
public boolean onJsAlert(WebView view, String url, String message, final JsResult result) {
//Check message for success/failure of payment
Log.i("message",message);
if (message.equals("Payment Fail")){
dgAlert("Payment Failed");
}
else if (message.equals("Payment Success")){
dgAlert("Payment Success");
}
return true;
}
}
这是我在点击“警报”对话框上的“确定”按钮时要做的事情
Intent intent=new Intent(Payment.this, DashBoardActivity.class);
startActivity(intent);
if (message.equals(Constants.CANCELLED)) {
activity.finishAffinity();
System.exit(0);
} else
activity.finish();
答案 0 :(得分:0)
我知道已经晚了,但是可能会帮助别人。您可以像这样在WebChromeClient中尝试返回false:
private class MyJavaScriptChromeClient extends WebChromeClient {
@Override
public boolean onJsAlert(WebView view, String url, String message, final JsResult result) {
//Check message for success/failure of payment
Log.i("message",message);
if (message.equals("Payment Fail")){
dgAlert("Payment Failed");
}
else if (message.equals("Payment Success")){
dgAlert("Payment Success");
}
return false;
}
}
它将正常工作。
答案 1 :(得分:0)
我遇到了同样的问题,多次重新加载 url 解决了问题,在 WebViewClient 中覆盖以下方法特别是错误相关的方法并在遇到错误时重新加载 url,保持最大重新加载阈值以避免无限重新加载错误循环。还要确保在使用后正确释放/销毁 webview 以避免内存泄漏。
override fun onReceivedSslError(
view: WebView?,
handler: SslErrorHandler,
error: SslError?
) {
Log.e("webview", "onReceivedSslError")
handler.proceed() // Ignore SSL certificate errors
}
override fun onPageStarted(view: WebView?, url: String?, favicon: Bitmap?) {
super.onPageStarted(view, url, favicon)
Log.e("webview", "onPageStarted")
onPageLoadStarted = true
progressBar.visibility = View.VISIBLE
}
override fun onReceivedError(
view: WebView?,
request: WebResourceRequest?,
error: WebResourceError?
) {
super.onReceivedError(view, request, error)
Log.e("webview", "onReceivedError" + error.toString())
progressBar.visibility = View.GONE
reload()
}
override fun onReceivedHttpError(
view: WebView?,
request: WebResourceRequest?,
errorResponse: WebResourceResponse?
) {
super.onReceivedHttpError(view, request, errorResponse)
Log.d("webview", "onReceivedHttpError" + errorResponse?.toString())
progressBar.visibility = View.GONE
reload()
}
并为活动或片段添加重新加载功能并覆盖生命周期方法,如下所示
var reloadCount=0
const val MAX_RELOAD_TRY = 6
fun reload() {
if ((reloadCount <= MAX_RELOAD_TRY)) {
reloadCount++
webView?.clearCache(true);
webView?.clearView();
webView?.clearHistory()
webView?.resumeTimers()
webView?.reload()
}
}
override fun onPause() {
super.onPause()
webView?.onPause()
}
override fun onResume() {
super.onResume()
webView?.onResume()
}
override fun onBackPressed() {
webView?.let {
if (it.canGoBack()) {
it.goBack()
} else {
super.onBackPressed()
}
} ?: run {
super.onBackPressed()
}
}
override fun onDestroy() {
super.onDestroy()
Log.d("onDestroy", "onDestroy")
destroyWebView()
}
private fun destroyWebView() {
Log.d("destroyWebView", "destroyWebView");
// Make sure you remove the WebView from its parent view before doing anything.
webView?.clearHistory()
// NOTE: clears RAM cache, if you pass true, it will also clear the disk cache.
// Probably not a great idea to pass true if you have other WebViews still alive.
webView?.clearCache(true)
// Loading a blank page is optional, but will ensure that the WebView isn't doing anything when you destroy it.
//webView?.loadUrl("about:blank")
// NOTE: This pauses JavaScript execution for ALL WebViews,
// do not use if you have other WebViews still alive.
// If you create another WebView after calling this,
// make sure to call mWebView.resumeTimers().
webView?.pauseTimers()
webView?.removeAllViews()
webViewContainerRL?.removeAllViews()
webView?.destroy()
webViewClient = null
webChromeClient = null
webView = null
}