Android Intent Example With Source Code

Android Intent is an object, which contains a message of an action to be performed. An intent is used to communicate between different components of application such as Services, Broadcasts and Activities, Services. An intent is a powerful feature. We can also interact with components of other application by using Intent. In this tutorial, we will use Intent in different ways and also pass data along with Intent.

  • Starting an Activity.
  • Starting and Stoping a Service
  • Sending data to another Activity
  • Receiving data from another Activity

You can find the complete video demonstration at the end of this article.

Download Complete Project Source Code

Starting Activity using Intent

In this example code we have two activities MainActivity and SecondActivity. We will use startActivity method to go from MainActivity to SecondActivity. Have a look at below code

First, we create the Explicit Intent and then call the startActivity method. It will open the SecondActivity.We can also send some data to SecondActivity by using a putExtra method. We have Explicit and Implicit Intents in Android.

Explicit Intent

Explicit Intents are used to call specific component of the same application. Explicit Intents are used when we know which component to launch such as starting a specific activity.We know the name of the activity as it is within the application not outside the application.

Implicit Intent

Implicit Intents are used to interact with the components outside the application where we don’t know the exact name of the component.Open an URL in the android browser or opening the Android Wifi Setting and the main Setting screen is the example of Implicit Intent.

Sending data to another Activity

We can pass the data of different type such as Integer,String,Boolean and String array to another activity by using putExtra method. Have a look at below code.

How To Start and Stop a Service Using Intent

Service is a component of android application. There is no interaction with user as it runs in the background. Service does not have any interface. Starting and stoping a service is very simple as shown in below code.

This is the complete code of It is a simple service that will print the data to logcat.

Here is the complete code of There are five Buttons. Each button has different Intents implementation.

Receiving data from another Activity

We can receive the data in SecondActivity that was sent from MainActivity by using android intent. We will use getIntent method to retrieve the data. Have look at below code showing how to use getIntent method.

getIntent() allows us to extract the data from Intent. We use getStringExtra method to retrieve string data. It has one argument which is the key “Name”.

Similarly, We have different methods to get different type of data. It depends on your data type which method to use. These methods have two arguments one is the key and second additional argument is the default value. If getIntExtra/getBooleanExtra methods don’t get the data or data were not pass from the calling activity. It will return the default value. In case of getStringExtra we will get the null value.

The getStringArrayExtra method is used to get the array and other types of data such as Hashmap.

Next step is to merge data into a single string and displaying into TextView. Here is the output of the SecondActivity.

android intent example

Android Intent Example – Video Demo

I have added a video tutorial in order to demonstrate all the steps involved in Android Intent Example project. This tutorial was developed using Android Studio 2.2.2.

I hope you will like this article on Android Intent and let us know your feedback via comments on this website and as well as on our Facebook Page and Youtube.

Check Also

asynctask kotlin example

Kotlin AsyncTask Tutorial with Example

Here is an example of implementing AsyncTask in Kotlin. I will create an Android application …