Android Intents Tutorial with Kotlin

In this Intents tutorial you’ll learn what Intents are, the role they play in Android, and how to use them to communicate with other installed apps. By Jenn Bailey.

Leave a rating/review
Download materials
Save for later
Share
You are currently viewing page 4 of 4 of this article. Click here to view the first page.

Intent Filtering

By now you should have a good idea of how to use the right intent for the right job. However, there’s another side to the story of the faithful intent: How your app knows which intent requests to respond to when an implicit intent is sent.

Open AndroidManifest.xml found in app/manifests and, in the first activity element, you should see the following:

<activity
    android:name=".TakePictureActivity"
    android:label="@string/app_name"
    android:screenOrientation="portrait">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

    <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>

</activity>

The key here is the intent-filter element. An Intent Filter enables parts of your app to respond to implicit intents.

These behave like a banner when Android tries to satisfy an implicit intent sent by another app. An app can have multiple intent filters, which it waves about wildly, hoping its intent filter satisfies what Android is looking for:

IntentFiltering

It’s kind of like online dating for intents and apps. :]

To make sure it’s the right app for the intent, the intent filter provides three things:

  1. Intent Action: The action the app can fulfill. This is similar to the way the camera app fulfills the ACTION_IMAGE_CAPTURE action for your app.
  2. Intent Data: The type of data the intent can accept. This ranges from specific file paths, to ports and to MIME types such as images and video. You can set one or more attributes to control how strict or lenient you are with the data from an intent that your app can handle.
  3. Intent Category: The categories of intents that are accepted. This is an additional way to specify which actions can respond to an implicit intent.

It would be AWESOME to offer Memeify as an implicit intent to interacting with images from other apps and it’s surprisingly simple to do.

Add the following code directly underneath the first intent filter in your AndroidManifest.xml file:

<intent-filter>
    <action android:name="android.intent.action.SEND" />
    <category android:name="android.intent.category.DEFAULT" />
    <data android:mimeType="@string/image_mime_type" />
</intent-filter>

Your new intent filter specifies that your app will look for SEND action from an implicit intent. You use the default category as you don’t have any special use cases and you’re looking only for image MIME data types.

Now, open TakePictureActivity.kt and add the following to the end of the class:

private fun checkReceivedIntent() {
  val imageReceivedIntent = intent
  val intentAction = imageReceivedIntent.action
  val intentType = imageReceivedIntent.type
    
  if (Intent.ACTION_SEND == intentAction && intentType != null) {
    if (intentType.startsWith(MIME_TYPE_IMAGE)) {
      selectedPhotoPath =
          imageReceivedIntent.getParcelableExtra<Uri>(Intent.EXTRA_STREAM)
      setImageViewWithImage()
    }
  }

}

Here, you get the Intent that started the activity and retrieve its action and type. Then you compare these to what you declared in your intent filter, which is a data source with the MIME type of an image.

If it’s a match, then you get the image’s Uri. Query the Uri for the Bitmap using a helper method included with the starter project. Then, finally ask the ImageView to display the retrieved Bitmap.

Next, add the following line at the end of onCreate():

checkReceivedIntent()

The above code ensures that you will check if there is an intent every time the activity is created.

Build and run. Then, back out to the home screen and go to the Photos app, or the Gallery app if you’re using the emulator. Choose any photo, and tap the share button. You should see Memeify among the presented options:

share image

Memeify is ready and waiting to receive your photo! Tap Memeify and see what happens! Memeify launches with the selected photo already displayed in the ImageView.

Your app is now receiving intents like a boss!

Where to Go From Here?

Feel free to download the completed project to check it out using the Download Materials button found at the top or bottom of this tutorial.

Intents are one of the fundamental building blocks of Android. Much of the openness and intercommunication that Android takes pride in just wouldn’t be possible without them. Learn how to use intents well and you will have made a very powerful ally indeed.

If you want to learn more about intents and intent filters then check out Google’s Intents documentation.

If you have any questions or comments on this tutorial, feel free to join the discussion and post your comments below!