Intent Objects

An Intent object is a bundle of information. It contains information of interest to the component that receives the intent (such as the action to be taken and the data to act on) plus information of interest to the Android system (such as the category of component that should handle the intent and instructions on how to launch a target activity). Principally, it can contain the following:

Component name

The name of the component that should handle the intent. This field is a ComponentName object — a combination of the fully qualified class name of the target component (for example "com.example.project.app.FreneticActivity") and the package name set in the manifest file of the application where the component resides (for example, "com.example.project"). The package part of the component name and the package name set in the manifest do not necessarily have to match.

Constant

Target component

Action

ACTION CALL

activity

Initiate a phone call.

ACTION EDIT

activity

Display data for the user to edit.

ACTION MAIN

activity

Start up as the initial activity of a task, with no data input and no returned output.

ACTION SYNC

activity

Synchronize data on a server with data on the mobile device.

ACTION BATTERY LOW

broadcast receiver

A warning that the battery is low.

ACTION HEADSET PLUG

broadcast receiver

A headset has been plugged into the device, or unplugged from it.

ACTION SCREEN ON

broadcast receiver

The screen has been turned on.

ACTION TIMEZONE CHANGED

broadcast receiver

The setting for the time zone has changed.

See the Intent class description for a list of pre-defined constants for generic actions. Other actions are defined elsewhere in the Android API. You can also define your own action strings for activating the components in your application. Those you invent should include the application package as a prefix — for example: "com.example.project.SHOW COLOR".

The action largely determines how the rest of the intent is structured — particularly the data and extras fields — much as a method name determines a set of arguments and a return value. For this reason, it's a good idea to use action names that are as specific as possible, and to couple them tightly to the other fields of the intent. In other words, instead of defining an action in isolation, define an entire protocol for the Intent objects your components can handle.

The action in an Intent object is set by the setAction() method and read by getAction() .

The component name is optional. If it is set, the Intent object is delivered to an instance of the designated class. If it is not set, Android uses other information in the Intent object to locate a suitable target — see Intent Resolution, later in this document.

0 0

Post a comment