Adding a Broadcast Receiver

A broadcast receiver listens for relevant broadcast messages to trigger an event. Some examples of broadcasted events already sent from the OS are

■ The camera button was pressed.

■ A new application was installed.

A user-generated component can also send a broadcast, such as:

■ A calculation was finished.

■ A particular thread has started.

All broadcast receivers extend the abstract class BroadcastReceiver or one of its subclasses. The lifecycle of a broadcast receiver is simple. A single method, onReceive() ,is called when a message arrives for the receiver. After this method is finished, the BroadcastReceiver instance goes inactive.

A broadcast receiver normally initiates a separate component or sends a notification to the user in its onReceive() method, as discussed later in this chapter. If a broadcast receiver needs to do something more time-consuming, it should start a service instead of spawn a thread because an inactive broadcast receiver might be killed by the system.

Was this article helpful?

0 0
Character Building Thought Power

Character Building Thought Power

Character-Building Thought Power by Ralph Waldo Trine. Ralph draws a distinct line between bad and good habits. In this book, every effort is made by the writer to explain what comprises good habits and why every one needs it early in life. It draws the conclusion that habits nurtured in early life concretize into impulses in future for the good or bad of the subject.

Get My Free Ebook

Post a comment