Auto-refresh cache
We configure an alarm within BroadcastReceiver's onHandleIntent method that initiates a process terminating in a refresh of local cached data obtained from a cloud service. The process is run on a worker thread and avails of the Retrofit HTTP client to communicate across the network so as to keep the local cached data in sync with that of the service.
Manual-refresh cache
In an accompanying presentation we showed how to use an alarm, receiver and service to automatically refresh the local data cache. Here we intervene manually and by exercising a menu option make a direct call to the service to retrieve the latest data set with which the cache is updated.
BroadcastReceiver
We subclass BroadcastReceiver and within its onReceive method we retrieve the user-input refresh interval and set an alarm to fire at this frequency. Each firing results in downloading the latest service data set and refreshing the local cache. The presentation examines the onReceive method in fine detail.
MyRent-14 (Services)
We avail of two Android classes, BroadcastReceiver and IntentService, to asynchronously refresh local data obtained by querying a cloud-based service. The BroadcastReceiver receives intents sent by a Context method, sendBroadcast
. The IntentService handles asynchronous requests. Our approach is to initialize an alarm in the BroadcastReceiver to exercise the IntentService at interverals configurable from the settings. When the alarm fires, the local data is asynchronously refreshed.