Android intent example

This tutorial explains Android intent, its types and methods with examples.

Android intent

An intent is an object used to request an action from another component. Intent is a data structure holding an abstract description of operation to be performed. It is used to request functionalities from other Android components. In short words Intent is an intention to do something. We can do many things by using intent like navigate from one activity to another activity, take picture via camera app, web search, search location on map and much more. This allows developers to easily remix different apps.

Types of Intent

There are two types of intents, explicit and implicit intent.

Explicit Intent

In explicit intents, target component name is directly passed in the intent at the time of creating it. Explicit intent is used very commonly when an activity calls another activity.  For example we have two activities – login activity and homepage Activity, after login app takes user to home page, see the photo given below.

Explicit intent

Explicit intent

Here is the code how it should be called in login activity.

Intent intent = new intent(this, homepageActivity);

Implicit Intent

In explicit intents, target component name is not passed in the intent at the time of creating it. Android decides itself which component of which application should receive this intent. Explicit intents are used to activate components in other applications.

For example if your application needs to open contacts in your phone (this intent needs another application which is contacts in phone), so the code will look like

Intent intent = new Intent();

So it will open Android contacts as shown below

Android contacts

Android contacts

Methods of Intent

There are separate mechanisms for delivering intent to activities, services and broadcast receivers. Here is explanation of few methods

Context.startActivity(): This method is used to launch a new activity, intent is passed as a parameter.

Context.startService(): This method is used to launch a new service, intent object is passed as a parameter.

Context.sendBroadcast(): This method is used to send message to any broadcast receiver. Intent object is passed as a parameter.

There are two primary components of intent object.

Action: It shows the action to be performed. It is compulsory part of intent object. The action to be performed can be ACTION_VIEW, ACTION_EDIT etc.

Data: It shows the data to be operate on. It can be a simple data type or  URI.

Example of intent

Here is an example which shows intent. Go to your Android Studio and create a new project. This example will open in your Android.

Here is activity_main.xml

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout android:layout_width="368dp"

            android:text="Visit" />

Here is

package com.example.admin.intentexample;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    Button button;
    protected void onCreate(Bundle savedInstanceState) {
        button = (Button)findViewById(;

        button.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                Intent i = new Intent(android.content.Intent.ACTION_VIEW,

Here is how it looks

intent example

intent example

Choose browser

Choose browser

Java Tutorial

Java Tutorial

You can download this code from this link.

0 0 votes
Article Rating
Inline Feedbacks
View all comments