I’m a starter in Android app development, and my experience in Java development is also minimal.
I’m working on a very basic context-aware application and I want to understand how can I notify my activity class of some custom event? (unlike onClick()). So if I have a class which is responsible for monitoring some resource. (For simplicity, let’s assume a Timer class which monitors time and notifies the Activity class every hour).
How can my activity class ‘subscribe’ and listen for such events from the Timer class?
I would really appreciate if you provide some code example and perhaps how event handling works in Android/Java. Thanks for your time and help!
Use
BroadcastIntents to notify listeners of system or application/custom events..send a
BroadcastusingsendBroadcastmethod whenever your custom event occurs..To create a new Broadcast Receiver(which will listen to your event/broadcast), extend the
BroadcastReceiverclass and override theonReceiveevent handler.TheonReceivemethod will be executed when aBroadcastIntent is received that matches the Intent Filter used to register the receiver.Register your receiver in either Manifest file or in the code..
//in xml
//in code..