Display an alert when internet connection not available in android application

后端 未结 13 663
有刺的猬
有刺的猬 2020-12-08 11:48

In my application data comes from internet and I am trying to create a function that checks if a internet connection is available or not and if it isn\'t, it gives an alert

相关标签:
13条回答
  • 2020-12-08 12:32

    Maybe try this

    handler.removeCallbacks(checkInternetConnection);
                    handler.postDelayed(checkInternetConnection, UPDATE_INTERVAL); 
    
    
    
        public Runnable checkInternetConnection = new Runnable() {
    
                public void run() {
    
                    handler.postDelayed(checkInternetConnection, UPDATE_INTERVAL);
                    ConnectivityManager conMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    
                    if(conMgr.getActiveNetworkInfo()!=null
                    && conMgr.getActiveNetworkInfo().isAvailable()
                    && conMgr.getActiveNetworkInfo().isConnected()){
                        alertOff();
    
                    }
                    else{
                        alertOn();
                    }
    
                }
            };
    
    0 讨论(0)
提交回复
热议问题