Search code examples
androidandroid-webview

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


I have an android webView application; I need it to show a custom HTML page when the internet is not available. I, also, want the user, if disconnected from the internet through the process of loading, can see my URL. So I just want to do something that user get my custom page whenever she/he got disconnected. I searched many answers but I didn't get the right one.


Solution

  • You can use RxJava/RxAndroid to check if internet connection is available or not. This answer should provide you with all the codes required to implement. Or, you can use ReactiveNetwork and observe Connectivity with observeNetworkConnectivity(context) method like the following:

    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 
            }
       );
    

    If you want to implement it using Android Architecture Components like LiveData, please go through this code and for details, this article