Mobile Application Developer

Home » , » Check if internet is available : Android

Check if internet is available : Android

Written By Mitul Nakum on Wednesday, March 16, 2011 | 3:49 PM

This function will return true if you can access the internet (or false if you can't). You can change the parameter being return when roaming (read the comments).
/*
* @return boolean return true if the application can access the internet
*/
private boolean haveInternet() {
NetworkInfo info = ((ConnectivityManager)getSystemService
(Context.CONNECTIVITY_SERVICE))
.getActiveNetworkInfo();
if (info == null || !info.isConnected()) {
return false;
}
if (info.isRoaming()) {
// here is the roaming option you can change it if you want to
// disable internet while roaming, just return false
return true;
}
return true;
}

In your manifest file add at least:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

About Mitul Nakum

15+ years of experience in mobile application development which includes Symbian, J2ME, Android and iOS development

1 comments :

  1. I think this class is use at first level check for connectivity if cell tower is available or not.

    If network coverage is full but user is not subscribed to internet or balance is over will this work ?

    I think at second level to check the connectivity to server best option is to check to try to connect to server if your app is network based. Other wise check for any google based small size resource for 200 response code. That will work for sure.

    Thanks for sharing mitul.

    ReplyDelete