Intent

Intent is basically a messaging object used to request another component of application and to send message between them. You can also say it provides communication between two components of application. Intent provides communication of components by three of following ways,

1. Starting Activities

Activity is nothing but a screen which is visible to you when you open any application. Here intent is used to start activity using startActivity() method. For example, if you want to open new screen on button click, Intent will help you perform this. Also we can pass data from one activity to another activity using Intent. For this please see my example.

2. Starting Services

Services is basically a process which is perform in background of application without user interface and interaction. For example, you get notification in your application.

3. Delivering Broadcast

It is a message that you can pass while something happens with your phone i.e SMS received or Phone ringing.

Types of Intent

There are two types of Intent available in Android.

1. Explicit Intent

Explicit Intent specify the name of android component which is going to call. Typically this intent used internally in application. For example, you want to start a new activity form your app.

Intent i = new Intent(FirstActivity.this, SecondActivity.class);  

startActivity(i);

Follow below steps to implement Explicit Intent.

Storing and Passing value to Intent using putExtra()

Step 1. Create new project with empty activity call MainActivity. I have created project with name IntentExample. Also create another activity called SecondActivity.

Step to create new Activity: Right click on app-> New-> Activity-> Empty Activity-> Give proper name and Finish.

Modify activity_main.xml file according to your requirements. Here I have created form with two EditText and one Button. On clicking of Button I will redirect activity from MainActivity to SecondActivity and also pass a values entered in EditText.

activity_main.xml

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

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:gravity="center_horizontal"

    android:orientation="vertical"

    android:padding="15dp"

    tools:context=".MainActivity">

  

    <EditText

        android:id="@+id/edtFullName"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:layout_marginBottom="5dp"

        android:hint="Enter fullname"

        android:padding="10dp" />

  

    <EditText

        android:id="@+id/edtUserName"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:layout_marginBottom="5dp"

        android:hint="Enter username"

        android:padding="10dp" />

  

    <Button

        android:id="@+id/btnRedirect"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:layout_marginBottom="5dp"

        android:text="Redirect" />

  

</LinearLayout>

Step 2. Open MainActivity.java, modify it by writing code to start SecondActivity and passing values of Fullname and Username. See below code and read my comments to understand it properly.

In this code Intent i=new Intent() is used to start new activity and putExtra() is used to store value which we want to pass in second activity. putExtra() method come with two parameters, first one for Key (i.e Used to recognize each record unique) and second one for value (i.e Contains value).

MainActivity.java

package com.example.intentexample;

  

import android.content.Intent;

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.EditText;

  

public class MainActivity extends AppCompatActivity {

  

    /* Declare object to store controls of activity_main.xml file */

    EditText edtFullName, edtUserName;

    Button btnRedirect;

  

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

  

        /* Find controls from activity_main.xml file and initialize it to object */

        edtFullName = (EditText) findViewById(R.id.edtFullName);

        edtUserName = (EditText) findViewById(R.id.edtUserName);

        btnRedirect = (Button) findViewById(R.id.btnRedirect);

  

        /* Redirect button click event */

        btnRedirect.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View v) {

  

                /* Getting user entered values from edittext */

                String fullName = "", userName = "";

                fullName = edtFullName.getText().toString();

                userName = edtUserName.getText().toString();

  

                /* Redirecting to Second activity with fullname and username */

                Intent i = new Intent(MainActivity.this, SecondActivity.class);

                i.putExtra("FullName", fullName); /* Storing value to putExtra() */

                i.putExtra("UserName", userName);

                startActivity(i);

            }

        });

    }

}

Retrieving values from Intent using getExtras()

Step 1. As above step show that, value will be passed to SecondActivity. In this step we retrieve that values from Intent in SecondActivity using getExtra()

If you do not have created SecondActivity, please create it. Step is already given above in step 1.

activity_second.xml

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

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:gravity="center_horizontal"

    android:orientation="vertical"

    android:padding="15dp"

    tools:context=".SecondActivity">

  

    <TextView

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_marginBottom="15dp"

        android:text="Welcome To My App"

        android:textSize="20dp"

        android:textStyle="bold" />

  

    <LinearLayout

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:layout_marginBottom="20dp"

        android:orientation="horizontal">

  

        <TextView

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:layout_marginBottom="5dp"

            android:text="Fullname : "

            android:textSize="16dp" />

  

        <TextView

            android:id="@+id/txtSecondFullName"

            android:layout_width="match_parent"

            android:layout_height="wrap_content"

            android:layout_marginBottom="5dp"

            android:text=""

            android:textSize="16dp"

            android:textStyle="bold" />

  

    </LinearLayout>

  

    <LinearLayout

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:layout_marginBottom="20dp"

        android:orientation="horizontal">

  

        <TextView

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:layout_marginBottom="5dp"

            android:text="Username : "

            android:textSize="16dp" />

  

        <TextView

            android:id="@+id/txtSecondUserName"

            android:layout_width="match_parent"

            android:layout_height="wrap_content"

            android:layout_marginBottom="5dp"

            android:text=""

            android:textSize="16dp"

            android:textStyle="bold" />

  

    </LinearLayout>

  

</LinearLayout>

Step 2. Write below code in SecondActivity.java to retrieve values from Intent.

In this code Bundle extras = getIntent().getExtras() is used to get all the key-value pairs passed from MainActivity and extras.getString("FullName") is used to get value of fullname using it KEY i.e FullName.

SecondActivity.java

package com.example.intentexample;

  

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.widget.TextView;

  

public class SecondActivity extends AppCompatActivity {

  

    /* Declare object to store controls of activity_main.xml file */

    TextView txtSecondFullName, txtSecondUserName;

  

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_second);

  

        /* Find controls from activity_main.xml file and initialize it to object */

        txtSecondFullName = (TextView) findViewById(R.id.txtSecondFullName);

        txtSecondUserName = (TextView) findViewById(R.id.txtSecondUserName);

  

        /* Retrieving Extras passed from MainActivity */

        Bundle extras = getIntent().getExtras();

        if (extras != null) {

            String fullName = "", userName = "";

            fullName = extras.getString("FullName"); /* Retrieving values from extras using KEY */

            userName = extras.getString("UserName");

  

            txtSecondFullName.setText(fullName);

            txtSecondUserName.setText(userName);

        }

    }

}

Output

2. Implicit Intent

Implicit Intent do not specify a name of android component which should be called, but it specify the action to be performed. For example, if you want to open url in browser you can use this Intent.

Intent intent=new Intent(Intent.ACTION_VIEW);  

intent.setData(Uri.parse("http://www.stdioh.in"));  

startActivity(intent);  

Follow below steps to implement Explicit Intent.

Call URL of any website and open it to browser using Intent

Step 1. Create new activity with name CallurlActivity and write below code to open url in browser. Here user will enter url of any website in given EditText and clicking on button will open it to browser.

activity_callurl.xml

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

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:gravity="center_horizontal"

    android:orientation="vertical"

    android:padding="15dp"

    tools:context=".CallurlAcitivity">

  

    <EditText

        android:id="@+id/edtURL"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:layout_marginBottom="5dp"

        android:hint="http://www.example.com"

        android:padding="10dp" />

  

    <Button

        android:id="@+id/btnOpenURL"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:layout_marginBottom="5dp"

        android:text="Open URL" />

  

</LinearLayout>

CallurlAcitivity.java

package com.example.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;

import android.widget.EditText;

  

public class CallurlAcitivity extends AppCompatActivity {

  

    EditText edtURL;

    Button btnOpenURL;

  

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_callurl);

  

        edtURL = (EditText) findViewById(R.id.edtURL);

        btnOpenURL = (Button) findViewById(R.id.btnOpenURL);

        btnOpenURL.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View v) {

                String url = edtURL.getText().toString();

  

                Intent intent = new Intent(Intent.ACTION_VIEW);

                intent.setData(Uri.parse(url));

                startActivity(intent);

            }

        });

    }

}

Output

Your Project Structure may look like,

I hope you like this blog. If you have any query, please comment below. Thank You… :)