如何检查WebView是否为白屏?

问题描述 投票:0回答:1

我正在尝试实现一个监控系统,可以检查WebView(在Android或iOS应用程序中)的网页是否是用户客户端的白屏,因此我们可以收集网页错误日志并向我们的开发团队报告以改进我们的-app-webpages的稳定性。

android ios android-webview frontend wkwebview
1个回答
0
投票

如果要在WebView中获取已加载的html页面的背景颜色,则必须执行下一步:

  1. 声明javascript函数 private static final String SHOW_COLOR_SCRIPT = "var color = window.getComputedStyle( document.body, null). getPropertyValue('background-color');Android.showToast(color);";
  2. 创建javascript界面 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(); } }
  3. 在您的webview中启用javascript并设置javascript界面 mWebView.getSettings().setJavaScriptEnabled(true); mWebView.addJavascriptInterface(new WebAppInterface(this), "Android");
  4. 运行声明的SHOW_COLOR_SCRIPT以使用方法显示具有html正文背景颜色的Toast 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

© www.soinside.com 2019 - 2024. All rights reserved.