👨‍💻Using Intents in 7 Steps | What is Intent? Why Should I Know About It?

Ali Osman ARSLAN
Huawei Developers
Published in
8 min readApr 18, 2023
Using Android Intent

Introduction

Hello, When developing Android applications, you may need to perform operations such as switching between different activities or communicating with other applications. In Android, a mechanism called “Intent” is used for such operations. Intents are a mechanism used to facilitate interaction between application components.

In this article, we will explain how to use Intents in the Kotlin language. Additionally, we will provide a few examples to demonstrate how Intent usage is done.

What is Intent?🙄

Intent is a message that allows one application component to interact with another component. In other words, an Intent is an object used to start an operation or send a data set to another component.

Intents are primarily used to facilitate interaction between Android applications. For example, a user may need another application to send an email from their email application. In this case, the email application can create an Intent and add the email data they want to send to this Intent. Afterwards, this Intent is processed by another application and the email sending operation is performed.

Concept of Intent

Types of Intents ✌️

There are two different types of Intents in Android: explicit and implicit Intents.

1. Explicit Intents

Explicit Intents explicitly specify the target component (activity, service, etc.). This type of Intent contains information that specifies a specific target, such as the name of a component or the package name and name of a component.

For example, in the following code snippet, an Explicit Intent instance is created from the MainActivity class to the MyOtherActivity class:

val intent = Intent(this, MyOtherActivity::class.java)
startActivity(intent)
Explicit Intents

This code creates an Intent to switch from MainActivity to MyOtherActivity and starts this Intent with the startActivity() method.

2. Implicit Intents

Implicit Intents are typically used to interact with an application or component selected by the user to perform an action. For example, when a user selects a gallery application to view a photo, this selection is given to an Implicit Intent and the appropriate application is launched. For example, opening a web page with an Implicit Intent:

val intent = Intent("https://www.aliosmanarslan.com/")
intent.data = Uri.parse(getWebSite.toString())
startActivity(intent)
Implicit Intents

The following code example demonstrates opening the gallery application to select an image using an Implicit Intent:

val intent = Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI)
startActivityForResult(intent, PICK_IMAGE_REQUEST)

This code snippet creates an implicit Intent example to pick an image from the MediaStore.Images.Media.EXTERNAL_CONTENT_URI database using the ACTION_PICK action and starts it with the startActivityForResult() method.

Using Intents🤔

Intents are often used to open another component, such as an activity or service, to perform an action or send a data set.

1. Opening Another Component

Using an Intent, another component (activity, service, etc.) can be opened. The following code snippet creates an Explicit Intent example to switch from the MainActivity class to the MyOtherActivity class and starts it with the startActivity() method:

val intent = Intent(this, MyOtherActivity::class.java)
startActivity(intent)

2. Performing an Action

An action can be performed using an Intent. For example, the following code creates an Implicit Intent instance to start a phone call:

val intent = Intent(Intent.ACTION_CALL, Uri.parse("tel:" + phoneNumber))
startActivity(intent)

3. Sending a Data Set

Using an Intent, a data set can be sent to another component. The following code snippet creates an Explicit Intent instance to send a string data set from the MainActivity class to the MyOtherActivity class:

val intent = Intent(this, MyOtherActivity::class.java)
intent.putExtra("myData", "Hello World!")
startActivity(intent)

This code snippet sends a string data with the key “myData” to the MyOtherActivity component.

4. Opening Camera Application

When you want to use the camera in an application, you can use an implicit intent to open the camera application. For example:

val intent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
startActivity(intent)

This code snippet creates an implicit intent example using the MediaStore.ACTION_IMAGE_CAPTURE action to open the camera application and starts it with the startActivity() method.

5. Making a Phone Call

You can use an implicit intent to make a phone call with a specific phone number. For example:

val phoneNumber = "1234567890"
val intent = Intent(Intent.ACTION_DIAL, Uri.parse("tel:$phoneNumber"))
startActivity(intent)

This code snippet creates an implicit intent example using the ACTION_DIAL action to make a phone call and starts it with the startActivity() method.

6. Opening a Web Page

You can use an implicit intent to open a web page. For example:

val webPage = "https://www.aliosmanarslan.com"
val intent = Intent(Intent.ACTION_VIEW, Uri.parse(webPage))
startActivity(intent)

This code snippet creates an implicit intent instance using ACTION_VIEW action to open a web page and starts it with the startActivity() method.

7. Receiving Data

You can use an explicit intent to receive data from another application. For example:

val intent = Intent(Intent.ACTION_GET_CONTENT)
intent.type = "image/*"
startActivityForResult(intent, PICK_IMAGE_REQUEST)

This code snippet creates an explicit intent example using ACTION_GET_CONTENT action to select an image from another application, and starts it using the startActivityForResult() method. The selected data is returned through the onActivityResult() method.

Intent with Service

A Service is a component used to execute a background process. By using Intents, you can start and stop a Service. For example, in a media player application, you can use a Service to play a song. Below is an example code snippet on how to use Intents with Services:

// Create an Intent to start the Service
val intent = Intent(this, MyService::class.java)
startService(intent)

// Create an Intent to stop the Service
val intent = Intent(this, MyService::class.java)
stopService(intent)

This code snippet demonstrates examples of using Intent to start or stop a Service. You can replace the “MyService” with the name of your own Service class in your application.

Using Intent with Broadcast

A broadcast is a message that is broadcasted within or outside of an application. You can use Intents to start a BroadcastReceiver component and listen for a broadcasted message. For example, in an alarm application, you can use a Broadcast to trigger an alarm. Below is an example code snippet that shows how to use Intent with Broadcast operations:

// Create an Intent to send a Broadcast
val intent = Intent("com.example.myapp.MY_ACTION")
sendBroadcast(intent)

// Create a BroadcastReceiver component to receive a broadcast.
class MyReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
if (intent.action == "com.example.myapp.MY_ACTION") {
// Handle the broadcast message
}
}
}

// Register the BroadcastReceiver component.
val receiver = MyReceiver()
val filter = IntentFilter("com.example.myapp.MY_ACTION")
registerReceiver(receiver, filter)

// Unregister the BroadcastReceiver component.
unregisterReceiver(receiver)

This code snippet demonstrates examples of using Intent to send and receive a Broadcast. You can use your own Broadcast message name instead of “com.example.myapp.MY_ACTION” in your application. Additionally, you can use the registerReceiver() and unregisterReceiver() methods to register and unregister the BroadcastReceiver component.

BroadcastReceiver

IntentFilter

IntentFilter is a tool used to indicate that an application is ready to handle a specific Intent. An IntentFilter can include a description, action, and data type of the Intent.

IntentFilter can also be used for broadcasts that an application publishes. Broadcast receivers can use IntentFilter to listen for a specific type of broadcast and perform appropriate actions based on the received broadcast.

Let’s explain with examples:

  1. An application wants to send a “new photo uploaded” notification to other applications. This notification can be made using an Intent and the broadcast type can be defined as “NEW_PHOTO_ADDED”. Other applications can listen to “NEW_PHOTO_ADDED” broadcasts using IntentFilter.
  2. An application wants to send data, such as a photo, to the camera application. The camera application can accept this data using an IntentFilter that defines the data type and perform a specific action.
  3. An application wants to determine its location on the phone. This application can determine its location using GPS data. Once the location is determined, the application can send an Intent broadcast. This broadcast can be captured by applications that listen with a specific IntentFilter, and appropriate actions can be taken.

IntentFilter is used to define an application’s broadcasts in a specific format and allows other applications to listen to these broadcasts. IntentFilter is an important tool in the Android application development process and has a flexible structure that can be used in different scenarios.

AndroidManifest.xml file to ensure that broadcasts are listened to correctly. This file contains information about the application’s package details and components, as well as information about which broadcasts to listen for. To define this information, the receiver tag is used, and anintent-filter is defined for each Receiver component.

For example, if we want to register a Broadcast Receiver to listen for the com.example.MY_ACTION broadcast type, we can use the following code snippet in the AndroidManifest.xml file:”

<manifest ... >
<application ... >
<receiver android:name=".MyBroadcastReceiver">
<intent-filter>
<action android:name="com.example.MY_ACTION" />
</intent-filter>
</receiver>
</application>
</manifest>

In the code snippet above, the MyBroadcastReceiver class is defined under the <receiver> tag. The intent-filter tag is used to listen for the com.example.MY_ACTION broadcast type. This way, when the application is launched, the MyBroadcastReceiver class will automatically listen for com.example.MY_ACTION broadcasts.

A Short Example from Daily Life 🧐

Imagine one day your friend sends you a message and asks, “Aloha! How’s the weather in Hawaii today?” You quickly create an Intent and open the “Hawaii Weather” app. Then, you learn that it’s a sunny day and perfect for swimming in the sea.

But it doesn’t end there! You also create an Intent to message your friend and say, “Aloha! The weather is beautiful, we’re waiting for you to come here too.” Your friend quickly creates an Intent and buys a ticket to come visit you. Together, you have fun swimming under the sun in Hawaii.

In conclusion, Intents can be very useful not only in the Android app development process but also in daily life. Well-designed Intents can lead you to create beautiful memories with your friends. So, use Intents correctly and open new doors in your life.

ALOHA!

Conclusion🍾

In this article, we learned about using Intent in the Kotlin language. We demonstrated how to use Intents with examples. Intents are an important mechanism for interactions between components in Android applications. Therefore, it is important to understand and use them correctly.

See you in our next article👋

References

--

--