X
    Categories: Android

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

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

Intent intent = new intent(this, homepageActivity);
startActivity(intent);

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();
Intent.setAction(android.content.intent.ACTION_VIEW);
Intent.setDeta(ContactsContract.Contacts.intent.CONTENT_URI);
startActivity(intent);

So it will open Android contacts as shown below

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 javatutorial.net in your Android.

Here is activity_main.xml

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


<RelativeLayout android:layout_width="368dp"
    android:layout_height="495dp"
    xmlns:tools="http://schemas.android.com/tools"
    tools:layout_editor_absoluteX="8dp"
    tools:layout_editor_absoluteY="8dp"
    xmlns:android="http://schemas.android.com/apk/res/android">

        <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="202dp"
            android:text="Visit JavaTutorial.net" />
    </RelativeLayout>

Here is ActivtyMain.java

package com.example.admin.intentexample;

import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    Button button;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button = (Button)findViewById(R.id.button);

        button.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent i = new Intent(android.content.Intent.ACTION_VIEW,
                Uri.parse("http://www.javatutorial.net"));
                startActivity(i);
            }
        });
    }
}

Here is how it looks

intent example

Choose browser

Java Tutorial

You can download this code from this link.

0 0 votes
Article Rating
NeelumAyub: Software Engineer
Related Post