Android - Programmatically check internet connection and display dialog if notConnected

后端 未结 14 2538
予麋鹿
予麋鹿 2020-12-15 04:04

I am working on a live project. and when user click on the app. the welcome screen appears(there is a webview on that screen). and if the internet is not connected then the

14条回答
  •  北荒
    北荒 (楼主)
    2020-12-15 04:57

    This is simple function that check your Internet connection. If Connected return true otherwise false.

     public boolean isInternetOn() {
    
            // get Connectivity Manager object to check connection
            ConnectivityManager connec =
                    (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    
            // Check for network connections
            if (connec.getNetworkInfo(0).getState() == android.net.NetworkInfo.State.CONNECTED ||
                    connec.getNetworkInfo(0).getState() == android.net.NetworkInfo.State.CONNECTING ||
                    connec.getNetworkInfo(1).getState() == android.net.NetworkInfo.State.CONNECTING ||
                    connec.getNetworkInfo(1).getState() == android.net.NetworkInfo.State.CONNECTED) {
    
    
                return true;
    
            } else if (
                    connec.getNetworkInfo(0).getState() == android.net.NetworkInfo.State.DISCONNECTED ||
                            connec.getNetworkInfo(1).getState() == android.net.NetworkInfo.State.DISCONNECTED) {
    
    
                return false;
            }
            return false;
        }
    }
    

    Here's a project that check internet connection and also check url that valid or contain in sever this.

提交回复
热议问题