In Android, Intents are messaging objects used to request actions from other app components, such as activities, services, and broadcast receivers. Intents facilitate communication both within an app and between apps. Intents can be categorized mainly into two types: Explicit Intents and Implicit Intents.
1. Explicit Intents
An Explicit Intent specifies the exact component (such as an activity or service) that should handle the intent. You use explicit intents to launch a specific activity or service within your own app.
Key Characteristics
- Directs the intent to a specific component.
- Commonly used within a single application, as you’re aware of the component’s name and package.
Usage Example
Starting a new activity using an explicit intent:
val intent = Intent(this, SecondActivity::class.java)
startActivity(intent)
Passing data between activities:
val intent = Intent(this, SecondActivity::class.java)
intent.putExtra("key", "value")
startActivity(intent)
In the receiving SecondActivity:
val bundle: Bundle? = intent.extras
val value = bundle?.getString("key")
2. Implicit Intents
An Implicit Intent does not specify a specific component. Instead, it declares a general action to perform, allowing any app that can handle that action to respond. The system identifies the appropriate app or component to handle the action.
Key Characteristics
- Flexible and allows interaction between different apps.
- The specific app or component that handles the intent is determined by the system based on the intent content and registered capabilities of other apps.
Common Actions
Intent.ACTION_VIEW: Used to display data to the user.
Intent.ACTION_SEND: Used to send data from one activity to another.
Intent.ACTION_DIAL: Opens the dialer with a specified phone number.
Intent.ACTION_WEB_SEARCH: Launches a web search.
Usage Example
Opening a web page using an implicit intent:
val uri = Uri.parse("http://www.example.com")
val intent = Intent(Intent.ACTION_VIEW, uri)
startActivity(intent)
Sharing content:
val shareIntent = Intent().apply {
action = Intent.ACTION_SEND
putExtra(Intent.EXTRA_TEXT, "This is my text to send.")
type = "text/plain"
}
startActivity(Intent.createChooser(shareIntent, "Share via"))
3. PendingIntent
A PendingIntent is a special type of intent that allows an external application to execute a predefined piece of code at a later time on behalf of your application. This is commonly used in situations such as notifications, where an app wants to execute some action even when it is not currently running.
Key Characteristics
- Useful for scheduling tasks, handling notifications, and communication with services.
- Grants another application the ability to perform an action on your app’s behalf.
Usage Example
Creating a PendingIntent for a notification:
val intent = Intent(this, MyActivity::class.java).apply {
flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
}
val pendingIntent: PendingIntent = PendingIntent.getActivity(this, 0, intent, 0)
val notification = NotificationCompat.Builder(this, CHANNEL_ID)
.setContentTitle("My notification")
.setContentText("Hello World!")
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setContentIntent(pendingIntent)
.setAutoCancel(true) // Automatically removes the notification when the user taps it
.build()
with(NotificationManagerCompat.from(this)) {
notify(NOTIFICATION_ID, notification)
}
4. Broadcast Intents
Broadcast Intents are used to broadcast a message to multiple receivers. There are two types of broadcasts:
- Normal Broadcasts: These are asynchronous and allow all receivers to run almost at the same time. Use
sendBroadcast.
- Ordered Broadcasts: These are synchronous and deliver the message to receivers one by one in a priority order. Use
sendOrderedBroadcast.
Usage Example
Sending a normal broadcast:
val intent = Intent("com.example.MY_CUSTOM_ACTION")
sendBroadcast(intent)
Receiving a broadcast in a BroadcastReceiver:
class MyBroadcastReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
// Handle the broadcast message here
}
}
// Registering the broadcast receiver
val filter = IntentFilter("com.example.MY_CUSTOM_ACTION")
registerReceiver(MyBroadcastReceiver(), filter)
Conclusion
Intents are a powerful and versatile part of the Android framework, enabling seamless communication between different components and applications. Understanding the various types of intents—explicit, implicit, PendingIntent, and broadcast intents—can significantly enhance an app’s functionality, providing a rich and interactive user experience. Each type of intent has its specific use cases and characteristics that developers can leverage to build robust and efficient applications.