I have implemented recurring alarms using the following example
However, once an alarm "goes off"/is recieved I want to update my ListView
in the active Activity. I only have one activity with a ListView
.
How do I execute my update UI method in the main activity class once an alarm is received? How do you call this from onReceive()
in the AlarmReceiver
(extends BroadcastReceiver
) class
The easiest way is to make your AlarmReceiver an inner class of your activity. This way it will have access to all fields and methods of your activity. If you don't use it anywhere else, it might as well be anonymous. To make it update your activity only when it's active, register your receiver in
onResume()
and unregister it inonPause()
. Notice theIntentFilter
specifying intent actions yourBroadcastReceiver
will respond to.Example:
If you still want your AlarmReceiver to be a separate class, pass some kind of callback to it during initialization:
Initialization of your
AlarmReceiver
will look the following way: