Friday, 26 February 2016

Intents and Broadcast Receivers in Android

Intents
  1. Three of the four component types - activities, services, and broadcast receivers - are activated by an asynchronous message called an intent. 
  2. Intents bind individual components to each other at runtime (you can think of them as the messengers that request an action from other components), whether the component belongs to your app or another. 
  3. An intent is created with an Intent object, which defines a message to activate either a specific component or a specific type of component - an intent can be either explicit or implicit, respectively.
    • An explicit intent can be to invoke another screen when a button is pressed on the Activity (UI) in context. 
    • An implicit intent is when you create an intent and hand it off to the system to handle it. 
  4. For activities and services, an intent defines the action to perform e.g. to "view" or "send" something. An intent might convey a request for an activity to show an image or to open a web page. 
  5. For broadcast receivers, the intent simply defines the announcement being broadcast (for example, a broadcast to indicate the device battery is low includes only a known action string that indicates "battery is low").

Broadcast Receivers
  1. A broadcast receiver is a component that responds to system-wide broadcast announcements. 
  2. Many broadcasts originate from the system—for example, a broadcast announcing that the screen has turned off, the battery is low, or a picture was captured. 
  3. Apps can also initiate broadcasts—for example, to let other apps know that some data has been downloaded to the device and is available for them to use. 
  4. Although broadcast receivers don't display a user interface, they may create a status bar notification to alert the user when a broadcast event occurs. 
  5. More commonly, though, a broadcast receiver is just a "gateway" to other components and is intended to do a very minimal amount of work. For instance, it might initiate a service to perform some work based on the event.
  6. A broadcast receiver is implemented as a subclass of BroadcastReceiver and each broadcast is delivered as an Intent object.

No comments:

Post a Comment