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
The 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
Activity
s 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
ConnectivityReceiver
class doc.
I have used this in the past by incorperating into a base class and using to trigger a Crouton
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!!
EDIT: (10/06/13) I have just seen that the guys at Novoda have created a small library to do this too. See Merlin on Github.