Whenever we want to switch or transfer data from one application to another application or one activity to another activity, at that time you have to use Intent.
An intent is an abstract description of an operation to be performed. It is a way of ‘Message passing between two or more than two components of android’. Different activities interact with each other with the help of intent in Android. In Android, basically, there are two type of intent.
Types of Intent:
- Implicit Intent
- Explicit Intent
In this blog post we will be explaining about Implicit Intent, in next blog we will be discussing on Explicit intent.
Implicit Intent
Intent is something which is sent from one activity to inbuilt android activity in android. When we work with implicit intents, we generally specify the action which we want to perform and optionally some data required for that action.
Data is typically expressed as a Uri which can represent an image in the gallery or person in the contacts database.
The amount of application that can be done using implicit intents are many. Some of the examples are as follows:
- Call
- Dialpad
- Contact
- Browser
- Call Log
- Gallery
- Camera
Running of application:
Download the code from the link provided above and follow the steps given in AndroidProjectInstall.pdf file to run the project on your PC.
There is a .apk file given in the live demo section. You can also download that file to run it on your Android devices (Mobile Phones).
Working:
In PC:
Right-click on your project, select ‘Run as’ and then select your desired device. Click on the Buttons that are created in your android application, they will perform the functions accordingly.
In Android Devices:
Download .apk file and install it. The app icon will start showing up on your menu section. Run the app and follow the steps as given above.
This application will start with several buttons, when you will click them the operations will be performed.
For eg: when you will click Call button, calling functionality will be performed.
Coding and designing of implicit intent:
XML codes
Now we will see XML coding of this application which is used for designing front view of application.
Activity_Main.xml :
XML file provides basic environment or design for your android application. By using different views and layouts you can design application of your choice. For this, you just have to know the basic XML Tags for designing interface.
<!-- LINEAR LAYOUT starts here -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@android:color/black"
android:orientation="vertical" >
<!-- EDIT TEXT with the hint of "Enter Phone No." -->
<EditText
android:id="@+id/etNo"
android:layout_width="match_parent"
android:layout_height="60dp"
android:hint="Enter phone no."
android:textColor="@android:color/holo_blue_light"
android:inputType="phone" />
<!-- BUTTON for the option Call -->
<Button
android:id="@+id/btnCall"
android:layout_width="match_parent"
android:layout_height="55dp"
android:textColor="@android:color/holo_orange_light"
android:text="Call" />
<!-- BUTTON for the option Camera -->
<Button
android:id="@+id/btnCamera"
android:layout_width="match_parent"
android:textColor="@android:color/holo_orange_light"
android:layout_height="55dp"
android:text="Camera" />
<!-- BUTTON for the option Contacts -->
<Button
android:id="@+id/btnContact"
android:layout_width="match_parent"
android:textColor="@android:color/holo_orange_light"
android:layout_height="55dp"
android:text="Contact" />
<!-- BUTTON for the option Browser -->
<Button
android:id="@+id/btnBrowser"
android:layout_width="match_parent"
android:textColor="@android:color/holo_orange_light"
android:layout_height="55dp"
android:text="Browser" />
<!-- BUTTON for the option Call log -->
<Button
android:id="@+id/btnCallLog"
android:layout_width="match_parent"
android:textColor="@android:color/holo_orange_light"
android:layout_height="55dp"
android:text="Call Log" />
<!-- BUTTON for the option Gallery -->
<Button
android:id="@+id/btnGallery"
android:layout_width="match_parent"
android:textColor="@android:color/holo_orange_light"
android:layout_height="55dp"
android:text="Gallery" />
<!-- BUTTON for the option Dialpad -->
<Button
android:id="@+id/btnDial"
android:layout_width="match_parent"
android:textColor="@android:color/holo_orange_light"
android:layout_height="55dp"
android:text="Dialpad" />
</LinearLayout>
Java codes
While we create our project at that time an XML file and a java file is created separately. MainActivity.java is the file where we write codes for java to implement logic and functionality in the app.
Mainactivity.Java
After you have designed your application using XML coding, you have to use the JAVA code so that help to perform action ( Dialpad, Call, Camera etc will open) when button will be clicked. Java file can be found in your project in Src folder.
//import required package
package com.implicitintent.mb;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.EditText;
//extends Activity
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//set click listener to camera button
findViewById(R.id.btnCamera).setOnClickListener(new View.OnClickListener() {
//perform camera open action
@Override
public void onClick(View v) {
Intent i = new Intent();
i.setAction(MediaStore.ACTION_VIDEO_CAPTURE);
startActivity(i);
}
});
final EditText et = (EditText) findViewById(R.id.etNo);
//set click listener to Gallery button
findViewById(R.id.btnGallery).setOnClickListener(new View.OnClickListener() {
//perform Gallery open action
@Override
public void onClick(View v) {
Intent i = new Intent();
i.setAction(Intent.ACTION_VIEW);
i.setData(Uri.parse("content://media/external/images/media/"));
startActivity(i);
}
});
//set click listener to CallLog button
findViewById(R.id.btnCallLog).setOnClickListener(new View.OnClickListener() {
//perform CallLog open action
@Override
public void onClick(View v) {
Intent i = new Intent();
i.setAction(Intent.ACTION_VIEW);
i.setData(Uri.parse("content://call_log/calls/1"));
startActivity(i);
}
});
//set click listener to Browser button
findViewById(R.id.btnBrowser).setOnClickListener(new View.OnClickListener() {
//perform Browser open action
@Override
public void onClick(View v) {
Intent i = new Intent();
i.setAction(Intent.ACTION_VIEW);
i.setData(Uri.parse("http://www.google.com/"));
startActivity(Intent.createChooser(i, "Title"));
}
});
//set click listener to Contact button
findViewById(R.id.btnContact).setOnClickListener(new View.OnClickListener() {
//perform Contact open action
@Override
public void onClick(View v) {
Intent i = new Intent();
i.setAction(Intent.ACTION_VIEW);
i.setData(Uri.parse("content://contacts/people/"));
startActivity(i);
}
});
//set click listener to Call button
findViewById(R.id.btnCall).setOnClickListener(new View.OnClickListener() {
//perform Call open action
@Override
public void onClick(View v) {
// AndroidManifest.xml -> Permissions -> Add ->
// Uses Permission -> android.permission.CALL_PHONE -> Save
Intent i = new Intent();
i.setAction(Intent.ACTION_CALL);
i.setData(Uri.parse("tel:"+et.getText()));
startActivity(i);
}
});
//set click listener to Dial button
findViewById(R.id.btnDial).setOnClickListener(new View.OnClickListener() {
//perform Dial open action
@Override
public void onClick(View v) {
Intent i = new Intent();
i.setAction(Intent.ACTION_DIAL);
i.setData(Uri.parse("tel:"+et.getText()));
startActivity(i);
Intent I = new Intent(Intent.ACTION_DIAL, Uri
.parse("tel:" + et.getText()));
startActivity(i);
startActivity(new Intent(Intent.ACTION_DIAL, Uri
.parse("tel:" + et.getText())));
}
});
}
}
Conclusion:
Intents are important part of application development. Hope you explore more and develop more android apps. For more updates and to learn more application keep visiting our website. 🙂
You may also like:-
First Android App – Make Your First Android App
One Reply to “Android Intent Example”
Hey fugo,
Great guide you got here! Really enjoyed the code you provided so that we could test out each type of intent. Something to keep note of though, is if you don’t use the createChooser() method of the Intent class, you are given the option to select a default application. This is a downside because in the future you won’t be able to change the application that handles that action.
So let’s say you choose Gmail for your default ACTION_SEND with a data type of “text/plain”. If you later want to use twitter or Facebook to perform this task, you can’t switch. However, if you use the createChooser(); method, it will prompt the user each and every time to select the desired application.
If anyone is interested in learning more about the createChooser() method, check out my blog at http://www.androidsyndicate.com
Erick Shaffer