Intents and Intent-filters in Android

Intents and Intent-filters in Android

If you are a beginner, you might have used this Intent thing when navigating from one activity to another. But, it is more than that... If you don't know what it is let me explain it to you in details.

In simple terms: Intents in Android are a way to express the intention to do something. Intents contain information about the action you want to perform and the data you want to pass to the component that will receive the intent.

Example: You can create an intent to start the camera app to take a picture.

In official words: An Intent is a messaging object you can use to request an action from another app components(that are Activity, Content Provider, Service, Broadcast receiver).

So, Intent is basically a requesting mechanism kind of thing, right? But... Where does it request to? Our ANDROID OS!!

If you want to interact/perform action from one component to another, then you will create an Intent object and specify your request/action. That's all you have to do! Your andorid OS will do the rest of the work for you.

If we have to request something to our component B from our component A, then we will create an Intent in A and start our request. Our Intent will be passed to our OS and then OS will check for the requested component if that is present or not. If it is, then OS will pass the Intent to B.

Plus, if there is any data passed through the Intent, B can access it too. When we are talking about JAVA we can use getIntent() and in KOTLIN, you can just access the incoming Intent using intent.

Types of Intents:

Explicit Intent: Explicit intents are a type of intent in Android programming that explicitly specify the target component that should receive the intent. In simpler terms, explicit intent is a type of intent that is directed toward a specific activity or component in an Android app.

For example, if want to access the A section of your app, you can use an explicit intent to directly open the A activity within the app. The app would know exactly which component to open because you have EXPLICITLY specified the target component.

Example code:

// Executed in an Activity, so 'this' is the Context
// The fileUrl is a string URL, such as "http://www.example.com/image.png"
val downloadIntent = Intent(this, DownloadService::class.java).apply {
    data = Uri.parse(fileUrl)
}
startService(downloadIntent)//start our service

Implicit Intent: Implicit intents are a type that allows you to specify the actions that an app should perform, without having to specify the exact activity to be performed.

It is like a schoolboy who wants to eat his lunch, but he does not specify exactly what he wants to eat. He just knows he wants food. The teacher knows the school has a canteen, so she takes him there and he chooses what he wants to eat.

Similarly, with implicit intents, the developer does not specify the exact activity that needs to be performed, but instead, the Android system determines the appropriate activity based on the intent's action and data so your OS can show all available options.

Example Code:

// Create the text message with a string.
val sendIntent = Intent().apply {
    action = Intent.ACTION_SEND
    putExtra(Intent.EXTRA_TEXT, textMessage)
    type = "text/plain"
}
startActivity(sendIntent)//will show all available options

Uses of Intent:

Android intents are used to communicate between different components of an Android app or between different apps. They allow you to perform a variety of actions and tasks, including:

  1. Starting an Activity: An intent can be used to start a new activity within an app or to start a different app altogether.

  2. Sending Data: An intent can be used to pass data between activities, services, or broadcast receivers. This allows you to share data between different components of your app or between different apps.

  3. Sending Broadcast Messages: An intent can be used to send broadcast messages to all components of an app or to broadcast receivers registered with the Android system. This is useful for communication between different components of an app or between different apps.

  4. Starting a Service: An intent can be used to start a service that runs in the background and performs a specific task.

  5. Delivering Notifications: An intent can be used to deliver notifications to the user, such as alerts, reminders, or updates.

  6. Launching another app for result: An intent can be used to launch the camera app and capture a photo or video.

Overall, Android intents provide a powerful and flexible mechanism for communication between different components of an app or between different apps.

Also, don't forget that our OS interacts with Intents so don't ever forget to add your app components in your manifest file, because that's how your OS will know the existence of your component.

Receiving an implicit intent:

If you want to advertise which implicit intents your app can receive, declare one or more intent filters for each of your app components with an <intent-filter> element in your manifest file.

Each app component includes an <intent-filter> element explicitly set a value for android:exported. This attribute indicates whether the app component is accessible to other apps.

NOTE: Don't export your app's components unnecessarily. For example, if you intend to launch an app component using an internal nested intent, set that component's android:exported attribute to false. Use a PendingIntent instead of a nested intent.

Inside the <intent-filter>, you can specify the type of intent to accept using one or more of these three elements:

  • <action>:Declares the intent action accepted, in the name attribute. The value must be the literal string value of an action, not the class constant.

  • <data>:Declares the type of data accepted, using one or more attributes that specify various aspects of the data URI (scheme, host, port, path) and MIME type.

  • <category>:Declares the intent category accepted, in the name attribute. The value must be the literal string value of an action, not the class constant.

For example, here's an activity declaration with an intent filter to receive an ACTION_SEND intent when the data type is text:

<activity android:name="ShareActivity" android:exported="false">
    <intent-filter>
        <action android:name="android.intent.action.SEND"/>
        <category android:name="android.intent.category.DEFAULT"/>
        <data android:mimeType="text/plain"/>
    </intent-filter>
</activity>

There are a lot more things to cover in Intents, but I think these are sufficient things to start with.

Reference: https://developer.android.com/guide/components/intents-filters

This article is a wrapper around them, so you MUST checkout official docs if you want to learn more about Intents.

Follow if you understand this concept and want to learn more Android-related topics.

#xDaysOfAndroid #AndroidWithSagar #android #androiddevelopment