Android – What is Intent Class

Android – What is Intent Class?

An Intent Class is a Data Structure that represents

1. An operation to be performed, or

2. An event that has occurred.

Intents provide a flexible language for specifying operations to be performed. E.g Pick a contact, Take a photo, Dial a phone number or show a map. Intent is constructed by on component that want some work done and is received by some activity that can perform that work.

Intent Fields

The kinds of information that can be specified by an Intent. For example we can talk about Intent Fields

1. Action

2. Data

3. Category

4. Type

5. Component

6. Extras

7. Flags

Actions are the strings that represent operations to be performed. Examples include

1. ACTION_DIAL – Dial a Number

2. ACTION_EDIT – Display Data to Edit

3. ACTION_SYNC – Synchronize device data with server.

4. ACTION_MAIN – Start as initial activity of app.

Setting the Intent Action.

We can pass an action intent to Intent class constructor.

Intent newInt = new Intent(Intent.ACTION_DIAL);

Or alternatively we can create an empty intent and pass an intent action to setAction method.

Intent newInt = new Intent();

newInt.setAction(Intent.ACTION_DIAL);

Data Field

Intent also has a data field that is data associated with intent and is formatted as Uniform Resource Identifier (URI).

One example is Geo schemed URI i-e Data to View on a map. or a tel schemed URI representing a Telephone number to Dial. The data is passed to Uri.parse method that take that string and returns a URI object.

Setting Intent Data

We can set intent data using constructor method of Intent class or creating an empty Intent object and then use the setData method to set the data.