Determining and Monitoring the Connectivity Status



Determine if You Have an Internet Connection


ConnectivityManager cm =
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
boolean isConnected = activeNetwork != null &&

Determine the Type of your Internet Connection



boolean isWiFi = activeNetwork.getType() == ConnectivityManager.TYPE_WIFI;



Monitor for Changes in Connectivity

The ConnectivityManager broadcasts the CONNECTIVITY_ACTION ("") action whenever the connectivity details have changed. You can register a broadcast receiver in your manifest to listen for these changes and resume (or suspend) your background updates accordingly.

<action android:name=""/>

Changes to a device's connectivity can be very frequent—this broadcast is triggered every time you move between mobile data and Wi-Fi. As a result, it's good practice to monitor this broadcast only when you've previously suspended updates or downloads in order to resume them. It's generally sufficient to simply check for Internet connectivity before beginning an update and, should there be none, suspend further updates until connectivity is restored.

This technique requires toggling broadcast receivers you've declard in the manifest, which is described in the next lesson.