Android Intent and its types with easy explanation

In this tutorial,we will learn about the intent and its types with easy explanation step by step.

This is a very important topic which is used every stage of android development phase.Here we will discuss all abouts the intents.

Let’s start

What is an  intent ?

Android intent is a message which is used to communicate with the android component such as passing data between activities,content provides,broadcast receiver,service etc.

Intents are generally used to launch the new activity using startActivity( ).

Intents are generally used

1.Launch the new activity

2.pass data from one activity to another activity

3.For broadcasting message

4.Display webpage 

5.Display list of contact in a Listview

6.Dial a phone call

7.etc

Types of intents in Android

There are two types of intents 

1.Implicit intent

2.Explicit intent

Implicit intent

Implicit intent is invoked by the system component.These component can be used in the different activity or where you want to use.

Example

Display webpage in android activity

Intent intent=new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(“https://www.placementpreparation.com”));
startActivity(intent);

Make a phone call using intent

Intent intent=new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse(“tel:”+999999999));
startActivity(intent);

Explicit intent

Explicit intent is not invoked by the system component.Here we have to provide own action in the activity or where you want to use.

Example:

Suppose passing data from one activity to another activity

Intent intent=new Intent(FirstActivity.this,secondActivity.class);
intent.putExtra(“name”,”suman”);
startActivity(intent);

Some standard activity actions

1.ACTION_MAIN

It is used to display the home screen of the application

<action android:name=”string_name”>

2.ACTION_DIAL

It is used to display the phone dialer with filled data.

Intent intent=new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse(“tel:”+9999999999));
startActivity(intent);

 3.ACTION_CALL

It is used to phone call directly with show caller number on phone.

Intent intent=new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse(“tel:”+9999999999));
startActivity(intent);

Permission Needed

<uses-permission android:name=”android.permission.CALL_PHONE”/>

4.ACTION_SEND

It is used to send text from one activity to another.

Intent intent=new Intent( );
intent.setAction(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_TEXT,”write here text as you want to send”);
intent.setType(“text/plain”);
startActivity(intent);

5.ACTION_PICK

It is used to pick image from the Gallery or Camera.

private static int SELECT_PHOTO=1;

Intent intent=new Intent(Intent.ACTION_PICK);
intent.setType(“image/*”);
startActivityForResult(intent,SELECT_PHOTO);

Example of intent

This is a simple example where we will pass the data from one activity from other activity.

1.First screen contains one EditText and one Button.

2.Second screen contain one TextView

first_activity.xml

<LinearLayout
android layout_heigth=”wrap_content”
android Orientation=”Vertical”
android layout_width=”wrap_content”>

<EditText
android id=”@+id/enter_data
android layout_heigth=”wrap_content”
android layout_width=”wrap_content”/>

<Button
android id=”@+id/button
android layout_heigth=”wrap_content”
android layout_width=”wrap_content”/>

</LinearLayout>

Second_activity.xml

<LinearLayout
android layout_heigth=”wrap_content”
android Orientation=”Vertical”
android layout_width=”wrap_content”>

<TextView
android id=”@+id/showdata
android layout_heigth=”wrap_content”
android layout_width=”wrap_content”/>

</LinearLayout>

This is the java class where we write the logic

FirstActivity.class

Public class FirstActivity extends Activity
{
private EditTex edittext,Button submit;
@Override
public void onCreate(Bundle SavedInstanceBundle)
{
super.onCreate(SavedInstanceBundle);
setContentView(R.layout.first_activity);

edittext=findViewById(R.id.enter_data);
submit=findViewById(R.id.button);

submit.setOnClickListener(new View.OnClickListener
@Override
Public void onClick(View v)
{
Intent intent=new Intent(Firstactivty.this,SecondActivity.class);
intent.putExtra(“name”,edittext.getText( ).toString( ));
startActivity(intent);
}
});
}
}

SecondActivity.class

Public class SecondActivity extends Activity
{
Private String nameText;
private EditTex edittext,Button submit;
@Override
public void onCreate(Bundle SavedInstanceBundle)
{
super.onCreate(SavedInstanceBundle);
setContentView(R.layout.second_activity);

name=findViewById(R.id.showdata);
nameText=getIntent.getStringExtra(“name”);
nameText.setText(name);
}
}

Output:

Hope,I explained all the concepts regarding the intents very well step by step.Still if you have any query you can comment me.

Thanks Regards

Placement preparation

sumankumar80

Hello Friends,I have written and developed this website because you can learn technology easily ,prepare for the placement and also for the any type of examination . By :Suman Kumar

Leave a Reply

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