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.

Intent newInt = new Intent(Intent.ACTION_DIAL, Uri.parse(“tel:+12345677”));

or

Intent newInt = new Intent(Intent.ACTION_DIAL);

newInt.setAction(Uri.parse(“tel:+12345677”));

Intent Category

Intent category represent additional information about the components that can handle Intent.

Examples include:

Category_Browseable  – Can be invoked by a browser to display data ref’s by a URI

Category_Launcher – Can be the initial activity of  a task & is listed in the top level app launcher.

Type Field

Intent also have a Type field that represent MIME Type of the Intent data.

Examples include  – IMAGE/*, IMAGE/PNG, IMAGE/JPG

TEXT/HTML, TEXT/PLAIN

Setting the Type.

Type can be set by setType method of Intent. Intent.setType(String Type);

We can also set data and type by calling setDataAndType method.

Intent.setDataAndType(Uri Data, String Type);

Component Field

Intent also has a component field that specify the component that should receive this Intent.

In next article we will explore Intent class in more detail.

 

Comments

  1. By Anna

    Reply

    • By Maz

      Reply

  2. By Muhammad Ali

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *