Android - Programmatically check internet connection and display dialog if notConnected

后端 未结 14 2486
予麋鹿
予麋鹿 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 05:09
     public boolean isOnline() {
            NetworkInfo activeNetworkInfo = ((ConnectivityManager) 
            getSystemService(CONNECTIVITY_SERVICE)).getActiveNetworkInfo();
            return activeNetworkInfo != null && 
            activeNetworkInfo.isConnectedOrConnecting();
        }
    
    0 讨论(0)
  • 2020-12-15 05:09
    private boolean isDeviceOnline() {
    
            ConnectivityManager connMgr = ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    
            NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
    
            return (networkInfo != null && networkInfo.isConnected());
        }
    

    This is the official way, Google also uses this same way to check the internet connection of the android device. You can check here - link. This is a very perfect way, it actually checks if the internet is working or not.

    Now your specific problem, Use a timer class then put a toast in that if(isdeviceOffline) then toast.

    0 讨论(0)
提交回复
热议问题