Android webView:持续检查互联网连接,直到加载 HTML

Android webView: check internet connectivity, continuously, till the HTML is loaded

我有一个 android webView 应用程序;我需要它在 Internet 不可用时显示自定义 HTML 页面。我也希望用户在加载过程中与互联网断开连接时,可以看到我的 URL。所以我只想做一些事情,让用户在 she/he 断开连接时获得我的自定义页面。 我搜索了很多答案,但没有找到正确的答案。

您可以使用 RxJava/RxAndroid 检查互联网连接是否可用。此 should provide you with all the codes required to implement. Or, you can use ReactiveNetwork 并使用 observeNetworkConnectivity(context) 方法观察 Connectivity,如下所示:

ReactiveNetwork
  .observeNetworkConnectivity(context)
  .subscribeOn(Schedulers.io())
  .observeOn(AndroidSchedulers.mainThread())
  .subscribe(
       connectivity -> {
        // do something with connectivity
        // you can call connectivity.state() or connectivity.type(); 
        }
       throwable    -> {
        // handle error here 
        }
   );

如果您想使用 Android 架构组件来实现它,例如 LiveData,请通过此 code and for details, this article