I registered a receiver that listens to network events:
My concern with the approach proposed by Aleksander is that it doesn't consider network changes of the same type, e.g. from one WiFi network to another.
I'm proposing to compare the extraInfo of the active network, which contains the network name, e.g. WiFi SSID or mobile network name like VZW
String currentNetworkName = "";
ConnectivityManager connectivityManager =
((ConnectivityManager) context.getSystemService(
Context.CONNECTIVITY_SERVICE));
NetworkInfo activeNetwork = connectivityManager.getActiveNetworkInfo();
boolean connected = activeNetwork != null && activeNetwork.isConnectedOrConnecting();
if (connected) {
// prevent duplicate connect broadcasts
String extraInfo = activeNetwork.getExtraInfo();
if(! currentNetworkName.equals(extraInfo)) {
// to do: handle network changes
currentNetworkName = extraInfo;
}
} else {
Log.d(TAG, "is not connected");
isConnected = false;
currentNetworkName = "";
}