Android: Connectivity Receiver
Here is a small snippit of how I handle Connectivity within some the applications I work on. A lot of apps dont seem to handle loss of connection correctly IMHO by either not showing a relevant error message or informing the user that there is an issue. Depending on the type of application, especially one that is meant to have a persistant network connection it can be useful to show some form of visual feedback when this is not the case.
The below is based upon Determining and Monitoring the Connectivity Status
ConnectivityReceiver helper class I use can be found on github.
The javadoc pretty much says it all but points to note are
- Remember to hook up to your
Activitys lifecycle methods
- Note that this is and always has been based upon a Sticky broadcast but this is not noted in the Android documentation. Something to be aware of that is mentioned in the
I have used this in the past by incorperating into a base class and using to trigger a
This is the way I default to handling connectivity issues but I would be interested to hear of other approaches - let me know if you do it another way!!