How do you check the internet connection in android? [duplicate]

Only one connection can be active at any one point. So a simpler answer is:

final ConnectivityManager conMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
final NetworkInfo activeNetwork = conMgr.getActiveNetworkInfo();
if (activeNetwork != null && activeNetwork.isConnected()) {
    // notify user you are online
} else {
    // notify user you are not online
} 

It also caters for any new type of network such as ConnectivityManager#TYPE_WIMAX


Also make sure that you have the required permission to monitor the network state. You need to add this permission to your AndroidManifest.xml:

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

Leave a Comment