Monday, December 20, 2010

Android #6: Working with Android Intents - Part 1

Android Intents are used for event notifications thus notifying applications about an event through passing messages between components/activities. In simple words, intents are messages that are passed between components. An Intent object holds the content of the message. For example, notifies when a SMS arrives, or SD card inserted into the device etc. Any component that wants to invoke another component has to express only through “intents” to perform a job.

Intents are typically used to activate components such as activities, services, and broadcast receivers. That is for launching an activity, broadcast messages for broadcast receivers, start a service/bind a service to communicate with any background service. Intents typically needs the basic information’s such as action to be performed and data to operate on.

  1. For activating the activity component, intent object holding the message content is passed to startActivity or startActivityForResult operations
  2. For activating the service component, intent object holding the message content is passed to startService operation
  3. For initiating a broadcast, intent object holding the message content is passed to sendBroadcast, sendOrderedBroadcast, and sendStickyBroadcast operations

Communication between components is achieved through intents. There are two primary form of intents namely

  1. Implicit Intents: Intents that do not name or specify a target component are termed as Implicit Intents. It is left to the platform to find an appropriate component to respond to the intent. Android platform resolves as to which component is most suitable to respond to such implicit intents using intent filters configured within Android Manifest file.
  2. Explicit Intents: Unlike Implicit Intents, you actually specify the target component that is required to respond to the intent.
Typical Intents Usage:

  • Inter-Activity (Activity-to-Activity) Communication: Here we can use Intents to pass information between activities wherever we have a need for creating multiple activities, and there exists a need for communicating/passing information between them. The activities can be implicit or explicit activities
  • Activity-Service Communication: Here we can use Intents to make a service perform a information update in an activity.
Examples:

Thought would create three different examples to show on inter-activity (activity-to-activity) communication in separate posts.

  1. Implicit Intents example: This example demonstrates the inter-activity communication and will show on invoking some of the built-in Android activities from one activity (http://satworks.blogspot.com/2010/12/android-7-working-with-android-intents.html)
  2. Explicit Intents example: This example demonstrates the inter-activity communication and will show on invoking user defined custom activities from one activity (http://satworks.blogspot.com/2010/12/refer-previous-post-to-get-background.html)
  3. Explicit Intents with return example: This example demonstrates the inter-activity communication and will show on invoking user defined custom activities from one activity but instead of passing the control to the invoked activity, will stay on the current invoking activity with values updated from invoked activity (http://satworks.blogspot.com/2010/12/android-9-working-with-android-intents.html)

No comments:

Post a Comment