Android Intents - Androhub

Intents

Android Intents

What are intents?

Intents are asynchronous messages which allow application components to request functionality from other Android components. Intents allow you to interact with components from the same applications as well as with components contributed by other applications. For example, an activity can start an external activity for taking a picture.

Intents are objects of the android.content.Intent type. Your code can send them to the Android system defining the components you are targeting. For example, via the startActivity() method you can define that the intent should be used to start an activity.

An intent can contain data via a Bundle. This data can be used by the receiving component.

Intents types

Different types of intents

types_of_intents

Android supports explicit and implicit intents.

An application can define the target component directly in the intent (explicit intent) or ask the Android system to evaluate registered components based on the intent data (implicit intents).

Explicit Intents

Explicit intents explicitly define the component which should be called by the Android system, by using the Java class as identifier.

The following shows how to create an explicit intent and send it to the Android system. If the class specified in the intent represents an activity, the Android system starts it.

Explicit intents are typically used within on application as the classes in an application are controlled by the application developer.

Implicit Intents

Implicit intents specify the action which should be performed and optionally data which provides content for the action.

For example, the following tells the Android system to view a webpage. All installed web browsers should be registered to the corresponding intent data via an intent filter.

If an implicit intent is sent to the Android system, it searches for all components which are registered for the specific action and the fitting data type.

If only one component is found, Android starts this component directly. If several components are identified by the Android system, the user will get a selection dialog and can decide which component should be used for the intent.

Example

VIDEO DEMO

Let’s start with an example of both kind of intents.

1. Create a new project in Eclipse by navigating to File ⇒ New Android ⇒ Application Project and fill required details. (I kept my main activity name as MainActivity.java)

2. For Second Activity we are creating a separate activity. Create a new class in your package and name it as Second_Activity.java

3. Open your your AndroidManifest.xml file and make your “MainActivity” as Launcher activity.

4. Create a layout file for MainActivity.java under res ⇒ layout folder. I named the layout file as activity_main.xml. This will be your first launching activity layout.

5. Add the following code in MainActivity.java activity. In this following code we use both Implicit and Explicit Intents and also learn how to pass values from one activity to another.

6. Create a layout file for second activity under res ⇒ layout folder. I named the layout file as second_activity.xml.

7. Add the following code in Second_Activity.java activity. In this following code we learn how to get the intent values.

8. Run the application and you will get the output as shown in video.

Thanks 🙂

 

Post comment

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