I have read that the "ConnectivityManager" class gives information about the network. But I am confused on how to implement the code. I need an efficient way to check internet, wifi and GPRS at a time. Thanks
Asked
Active
Viewed 351 times
2 Answers
1
And again: http://developer.android.com/reference/android/net/ConnectivityManager.html#CONNECTIVITY_ACTION You have to do something, nobody will write complete code for you. Read the documentation and you know when the event is fired and which you have to sort out.

neofu
- 108
- 7
-1
Simply use this function. call this function where you want to check that internet is available or not.
public boolean isNetworkConnected(Context mContext) {
final String DEBUG_TAG = "NetworkStatusExample";
ConnectivityManager connMgr = (ConnectivityManager) mContext.getSystemService(CONNECTIVITY_SERVICE);
boolean isWifiConn=false;
boolean isMobileConn=false;
Network nn=connMgr.getActiveNetwork();
Network[] networkinf=connMgr.getAllNetworks();
for (int a=0;a<networkinf.length;a++) {
NetworkCapabilities networkCapabilities = connMgr.getNetworkCapabilities(networkinf[a]);
if (networkCapabilities.hasTransport(networkCapabilities.TRANSPORT_CELLULAR)){
isMobileConn=true;
}
else if (networkCapabilities.hasTransport(networkCapabilities.TRANSPORT_WIFI)){
isWifiConn=true;
}
}
Log.i(DEBUG_TAG, "Wifi connected: " + isWifiConn);
Log.i(DEBUG_TAG, "Mobile connected: " + isMobileConn);
editor.putBoolean("isConnected",isMobileConn);
editor.apply();
if (isMobileConn || isWifiConn){
Log.i(DEBUG_TAG, "Network Status..." + isMobileConn);
return true;
}
else {
return false;
}
}

Ferrmolina
- 2,737
- 2
- 30
- 46