Intent and Its Type
×
Home Courses Community Support
Gray Matter

First Step Towards Android: Introduction »

Understanding Activities and Intents »

Hands On With UI Designing In Android (Controls-Views,Widgets and Layouts) »

Updating... »

Intent and Its Type

Intent

An android intent or an Intent object is a passive data structure that holds an abstract description of an action/operation which is going to performed.

Simply it is an abstract description of an operation to be performed.

An Android application can contain zero or more activities. Whenever your application has more than one activity then you may need to switch or  navigate from one activity to another. The component through which you navigate between the activities is known as intent.

Also Intent is the message which is passed between components such as activities, broadcast receivers, content providers, services etc. It is generally used with startActivity() method to invoke activity, broadcast receivers etc.

The dictionary meaning of intent is intention or purpose. So, it can be described as the intention to do action .The LabeledIntent is the subclass of android.content.Intent class.Android intents are mainly used to:

  • Start the service
  • Launch an activity
  • Display a web page
  • Display a list of contacts
  • Broadcast a message
  • Dial a phone call etc.

Intent can be used with startActivity to launch an Activity, broadcastIntent to send it to any interested Broadcast Receiver components, and startService(Intent) or bindService (Intent, ServiceConnection, int) to communicate with a background Service.

To understand the intent in the better manner let us try  to understand with the help of an example -

We are going to use the Activities of project created earlier, Do the following modifications to the statements in bold.

AndroidManifest.xml file -

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

<manifest xmlns:android=http://schemas.android.com/apk/res/android
package=”net.learn2develop.Activities”
android:versionCode=”1”
android:versionName=”1.0”>
<application android:icon=”@drawable/icon” android:label=”@string/app_name”>
<activity android:name=”.MainActivity”
android:label=”@string/app_name”
android:theme=”@android:style/Theme.Dialog” >
<intent-filter>
<action android:name=”android.intent.action.MAIN” />
<category android:name=”android.intent.category.LAUNCHER” />
</intent-filter>
</activity>
<activity android:name=”.Activity2”
android:label=”Activity 2”>
<intent-filter>
<action android:name=”net.learn2develop.ACTIVITY2” />
<category android:name=”android.intent.category.DEFAULT” />
</intent-filter>
</activity>
</application>
<uses-sdk android:minSdkVersion=”9” />


Now make a right click on the package name under the src folder and select New -> Class.
Name the new class file as Activity2 and then click Finish.
Now make the copy of the main.xml file by doing the right click on it and then select on copy. Again make a right click on the res/layout folder and then select paste. Now we need to change the name of the file that we have just created so name it activity2.xml.
 


 


 


 

activity2.xml file -

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

<LinearLayout xmlns:android=http://schemas.android.com/apk/res/android
android:orientation=”vertical”
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”> 

<TextView
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:text=”This is Activity 2!”
/>
</LinearLayout>


Now we are going to modify  Activity2.java

activity2.xml file -

package net.learn2develop.Activities;

import android.app.Activity;
import android.os.Bundle;
public class Activity2 extends Activity {

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity2);

}
}

 

Now we are going to modify MainActivity.java file

package com.example.Activities;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;
import android.view.KeyEvent;
import android.content.Intent;

public class MainActivity extends Activity {

String tag = “Events”;
/** Called when the activity is first created. */

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
//---hides the title bar---
//requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
Log.d(tag, “In the onCreate() event”);

}

public boolean onKeyDown(int keyCode, KeyEvent event){

if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER){
startActivity(new Intent(“com.example.ACTIVITY2”));
}
return false;

}

public void onStart() { //... }
public void onRestart() { //... }
public void onResume() { //... }
public void onPause() { //... }
public void onStop() { //... }
public void onDestroy() { //... }
}

 

Now run the project…


 

There are two types of intents -


 

Implicit intents

Implicit Intent doesn't specify the component. In such case, intent provides information of available components provided by the system that is to be invoked.

The java class which should be called implicit intent asked the system to perform a service without telling the system which java class should do this service. 

For example1 -

Intent intent=new Intent(Intent.ACTION_VIEW);  
intent.setData(Uri.parse("http://www.geeksdemy.com"));  
startActivity(intent);


Example2 -

Intent read1=new Intent();
read1.setAction(android.content.Intent.ACTION_VIEW);
read1.setData(ContactsContract.Contacts.CONTENT_URI);
startActivity(read1);

 


 

Now we will create one application which performs the action of implicit intent -

activity_main.xml -

<RelativeLayout xmlns:androclass="http://schemas.android.com/apk/res/android"  
xmlns:tools="http://schemas.android.com/tools"  
android:layout_width="match_parent"  
android:layout_height="match_parent"  
tools:context=".MainActivity" >  
<EditText  
android:id="@+id/editText1"  
android:layout_width="wrap_content"  
android:layout_height="wrap_content"  
android:layout_alignParentTop="true"  
android:layout_centerHorizontal="true"  
android:layout_marginTop="44dp"  
android:ems="10" />  
<Button  
android:id="@+id/button1"  
android:layout_width="wrap_content"  
android:layout_height="wrap_content"  
android:layout_below="@+id/editText1"  
android:layout_centerHorizontal="true"  
android:layout_marginTop="54dp"  
android:text="Visit" />  
</RelativeLayout>


MainActivity.java -

package org.sssit.implicitintent;  
import android.net.Uri;  
import android.os.Bundle;  
import android.app.Activity;  
import android.content.Intent;  
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);  
setContentView(R.layout.activity_main);  
final EditText editText1=(EditText)findViewById(R.id.editText1);  
Button button1=(Button)findViewById(R.id.button1);  
button1.setOnClickListener(new OnClickListener() {  
@Override  
public void onClick(View arg0) {  
String url=editText1.getText().toString();  
Intent intent=new Intent(Intent.ACTION_VIEW,Uri.parse(url));  
startActivity(intent);  
}
});  
}
}

 

Output


 


 


 

Explicit Intents

Explicit Intent specifies the component. In such case, intent provides the external class to be invoked.

Explicit intent is responsible to connect internal world of application, Like if you want to connect one activity to another activity, then we need to apply explicit intent.
 


 

For example -

Intent i = new Intent(getApplicationContext(), ActivityTwo.class);  
startActivity(i);


Now we will create one application which performs the action of explicit intent -

activity_main.xml -

<RelativeLayout xmlns:androclass="http://schemas.android.com/apk/res/android"  
xmlns:tools="http://schemas.android.com/tools"  
android:layout_width="match_parent"  
android:layout_height="match_parent"  
tools:context=".MainActivity" >  
<Button  
android:id="@+id/Button01"  
android:layout_width="wrap_content"  
android:layout_height="wrap_content"  
android:layout_alignParentLeft="true"  
android:layout_below="@+id/TextView01"  
android:layout_marginLeft="65dp"  
android:layout_marginTop="38dp"  
android:onClick="onClick"  
android:text="Call second activity" />  
<TextView  
android:id="@+id/TextView01"  
android:layout_width="wrap_content"  
android:layout_height="wrap_content"  
android:layout_alignLeft="@+id/Button01"  
android:layout_alignParentTop="true"  
android:layout_marginLeft="18dp"  
android:layout_marginTop="27dp"  
android:minHeight="60dip"  
android:text="First Activity"  
android:textSize="20sp" />  
</RelativeLayout>

 

activitytwo_main.xml -

<RelativeLayout xmlns:androclass="http://schemas.android.com/apk/res/android"  
xmlns:tools="http://schemas.android.com/tools"  
android:layout_width="match_parent"  
android:layout_height="match_parent"  
tools:context=".MainActivity" >  
<Button  
android:id="@+id/Button01"  
android:layout_width="wrap_content"  
android:layout_height="wrap_content"  
android:layout_alignParentLeft="true"  
android:layout_below="@+id/TextView01"  
android:layout_marginLeft="65dp"  
android:layout_marginTop="38dp"  
android:onClick="onClick"  
android:text="Call First activity" />  
<TextView  
android:id="@+id/TextView01"  
android:layout_width="wrap_content"  
android:layout_height="wrap_content"  
android:layout_alignLeft="@+id/Button01"  
android:layout_alignParentTop="true"  
android:layout_marginLeft="18dp"  
android:layout_marginTop="27dp"  
android:minHeight="60dip"  
android:text="Second Activity"  
android:textSize="20sp" />  
</RelativeLayout>


MainActivityOne.java -

package com.example.explicitintent2;  
import android.os.Bundle;  
import android.app.Activity;  
import android.content.Intent;  
import android.view.Menu;  
import android.view.View;  
import android.view.View.OnClickListener;  
import android.widget.Button;  
import android.widget.Toast;  
public class ActivityOne extends Activity {  

/** Called when the activity is first created. */  
@Override  
public void onCreate(Bundle savedInstanceState) {  
super.onCreate(savedInstanceState);  
setContentView(R.layout.activity_main);  
Button button1=(Button)findViewById(R.id.Button01);  
button1.setOnClickListener(new OnClickListener(){  

public void onClick(View view) {  
Intent i = new Intent(getApplicationContext(), ActivityTwo.class);  
i.putExtra("Value1", "Android By Geeksdemy");  
i.putExtra("Value2", "Simple Tutorial");  

// Set the request code to any code you like, you can identify the  
// callback via this code  
startActivity(i);  
}
});
}
}

 

MainActivityTwo.java:-

package com.example.explicitintent2;  
import android.app.Activity;  
import android.content.Intent;  
import android.os.Bundle;  
import android.view.View;  
import android.view.View.OnClickListener;  
import android.widget.Button;  
import android.widget.EditText;  
import android.widget.TextView;  
import android.widget.Toast;  
public class ActivityTwo extends Activity {  

/** Called when the activity is first created. */  
@Override  
public void onCreate(Bundle bundle) {  
super.onCreate(bundle);  
TextView tv=new TextView(this);  
tv.setText("second activity");  
setContentView(R.layout.activity_two);  
Bundle extras = getIntent().getExtras();  
String value1 = extras.getString("Value1");  
String value2 = extras.getString("Value2");  
Toast.makeText(getApplicationContext(),"Values are:\n First value: "+value1+  

"\n Second Value: "+value2,Toast.LENGTH_LONG).show();  
Button button1=(Button)findViewById(R.id.Button01);  
button1.setOnClickListener(new OnClickListener(){  
public void onClick(View view) {  
Intent i = new Intent(getApplicationContext(), ActivityOne.class);  
startActivity(i);  
}
});  
}
}


Output -


 


 


 

← Preview

Progress Dialog Example

Next →

Communication Between Activities

NOTES

Our mission is to provide highly effective and quality education via innovative solutions. Geeksdemy look forward to bridge the gap between in-demand technology and academics in order to deliver innovative, easy, interesting and affordable learning across the Globe.

Courses

  • Python
  • Game Development with Unity 3D
  • Arduino
  • PCB & Circuit Designing
  • Robotics and Embedded C
  • 8051/8052 Embedded Systems
  • QuadCopter & UAV
  • MATLAB with Robotics
  • Raspberry Pi
Learn On the Go!
Get the Android App
Get the iOS App