为 WebView 设置 TimeOut

设置 WebView 的 WebViewClient,在 WebViewClient::onPageStart 回调方法里:

private static final String TAG = "WebView";
private static final int TIME_OUT_PROGRESS = 30;
private static final int TIME_OUT_MILLIS = 2*1000;

@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
  super.onPageStarted(view, url, favicon);
  new Handler(Looper.getMainLooper())
    .postDelayed(new Runnable() {
      @Override
      public void run() {
        if (mWebView.getProgress() < TIME_OUT_PROGRESS) {
          Log.e(TAG, "connection time out ------ current progress is: ------" + mWebView.getProgress());
          // Todo timeout code logic
        }
      }
  }, TIME_OUT_MILLIS);
}

你可能感兴趣的:(为 WebView 设置 TimeOut)