Pages

Friday, May 17, 2013

Check internet connection on Android Phone


public boolean isInternetAvailable(){
//If not in activity
// ConnectivityManager cManager = (ConnectivityManager) Context.getSystemService(Context.CONNECTIVITY_SERVICE);

//if in Activity
ConnectivityManager cManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo nInfo = cManager.getActiveNetworkInfo();
if(nInfo == null)
return false;
else
return nInfo.isConnected();
}




and onclick event

 if(isInternetAvailable()){
        do something
        }else {
       
        Toast.makeText(getApplicationContext(), "You must be connected to the internet", Toast.LENGTH_LONG).show();
        }

No comments:

Post a Comment