Wednesday, June 29, 2011

Android Intents

1.1. Overview

Objects of type "android.content.Intent" are used to send asynchronous messages within your application or between applications. Intents allow to send or receive data from and to other activities or services. They also allow to broadcast that a certain event has occurred.
Intents are a powerful concept as they allow the creation of loosely coupled applications. Intents can be used to communicate between any installed application component on the device.
An Intent object can contain information for the receiving component. For example if your application calls via an Intent a browser it may send the URL to the browser component. An Intent also contain information for the Android system so that the Android system can determine which component should handle the request.

1.2. Implicit vrs Explicit Intents

Android supports explicit intents and implicit intents. Explicit intent names the component, e.g. the Java class which should be called.
Implicit intents asked the system to perform a service without telling the system which Java class should do this service. In constructing an implicit Intent you specify the action which should be performed and optionally an URI which should be used for this action. For example you could tell the system that you want to view (action) a webpage (URI). By starting an intent for this data the system would try to find an application which is registered for this event, e.g. a browwer. You can add more data to the Intent by adding "extras" to the Intent. These are key/value pairs.

1.3. Getting the Intent data in the called Activity

To get the Intent information in the called Activity use the method getIntent(). If the Activity was called via an implicit Intent you can receive the data and url from this Intent via getAction(), getData() and getExtras().

1.4. Intent Filter

The Android system will determine suitable applications for an implicit intent and if several applications exists offer the user the choice to open one. The determination is based on intent filters, e.g. the class "android.content.IntentFilter". Intent filters are typically defined via the "AndroidManifest.xml" file.
To react to a certain implicit intent an application component must register itself via an IntentFilter in the "AndroidManifest.xml" to this event. If a component does not define intent filters it can only be called by explicit intents.

1.5. Intents as event triggers

Intents can also be used to broadcast messages into the Android system. An Android application can register Broadcast Receivers to these events and react accordingly. The Android system also uses Intents to broadcast system events. Your application can also register to these system events, e.g. a new email has arrived, system boot is complete or a phone call is received and react accordingly.

1.6. Starting Activities and Sub-Activities

To start an activity use the method startActivity(Intent) if you do not need a return value from the called activity.
If you need some information from the called activity use the method startActivityForResult(). Once the called Activity is finished the method onActivityResult() in the calling activity will be called. If you use startActivityForResult() then the activity which is started is considered a "Sub-Activity".

1.7. Android Basics

The following assumes that you have already basic knowledge in Android development . Please check the Android development tutorial to learn the basics.

No comments:

Post a Comment