我正在尝试实现一个监控系统,可以检查WebView(在Android或iOS应用程序中)的网页是否是用户客户端的白屏,因此我们可以收集网页错误日志并向我们的开发团队报告以改进我们的-app-webpages的稳定性。
如果要在WebView中获取已加载的html页面的背景颜色,则必须执行下一步:
private static final String SHOW_COLOR_SCRIPT = "var color =
window.getComputedStyle( document.body, null).
getPropertyValue('background-color');Android.showToast(color);";
public class WebAppInterface {
Context mContext;
/** Instantiate the interface and set the context */
WebAppInterface(Context c) {
mContext = c;
}
/** Show a toast from the web page */
@JavascriptInterface
public void showToast(String toast) {
Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
}
}
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.addJavascriptInterface(new WebAppInterface(this), "Android");
private void runScript(String script) {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
mWebView.evaluateJavascript(script, null);
} else {
mWebView.loadUrl("javascript:" + script);
}
}
但是对于处理WebView错误,这是一个糟糕的决定。试着看看这里Detecting Webview Error and Show Message