Given an Android intent service whose job is do background network communication (e.g. make a REST call to synchronize data), when the intent service catches an IOException
, what is a good practice for recovering from the error?
Let's assume the amount of data transferred is small enough that we're content retry the network operation from scratch. If the device lost network connection, we want to be notified when connectivity is restored and try again. If we didn't lose connection, we assume the server or its network link is down, and want to try again after a delay.
It is not critical to complete the communication operation as soon as possible, but quicker does mean a better user experience, although it must be traded off against bandwidth usage and battery life.
Hopefully, this is a common requirement and the functionality is baked into Android. If so, where is it, or if not, what would the code to intelligently restart the intent service look like?