Android Receiving Data from another app in 2 steps

Myrick Chow
ITNEXT
Published in
4 min readMay 10, 2020

--

Flowchart of sharing

Android app can share data between apps easily. They include text, image, video and audio files, etc. For example, you can share an image from your Google Photo app to your Facebook app or Instagram app to create a post or a story. It is nowadays a common feature in Android social app.

Sharing between different social apps

To receive data from another app, it is necessary to configure your Activity at AndroidManifest.xml properly and handle the Intent containing the action and data sent from system.

Implement this sharing feature only needs 2 step and cost around 10 mins. I believe you would find this interesting and useful! Let’s start!

Step-by-Step Procedures

Step 1) Add <intent-filter> at AndroidManifest

We have to add 4 tags to the Activity that handles the incoming data in the AndroidManifest.xml. They are :

  1. <intent-filter>
    Declare that this Activity is applicable to handle system Intent with the action and data defined at the <action> and <data> tags respectively.
  2. <action>
    Define the IntentAction that this Activity can handle, i.e. android.intent.action.SEND to receive a single datum and android.intent.action.SEND_MULTIPLE to receive a list of data. The action field is used by system to filter out the apps that are available to handle the requested Intent.
  3. <category>
    android.intent.category.DEFAULT must be defined at the <category> else the Activity cannot receive any implicit Intent and therefore cannot be shown at the Sharing bottom sheet dialog!
  4. <data>
    Declare the type of MIME (Multipurpose Internet Mail Extensions) that this Activity can take care. There are several common types:
    1. text/* which includes text/plain, text/rtf, text/html, text/json
    2. image/*which includes image/jpg, image/png, image/gif
    3. video/* which includes video/mp4, video/3gp
    4. audio/* which includes audio/wav
    5. application/pdf which includes pdf file

At this moment, you should be able to see your app icon appears at the sharing dialog. The bold title is the application name and the regular subtitle is the Activity name declared at the AndroidManifest.xml. See below:

Step 2) Handle data at Activity

After user clicks at the your app icon at the Sharing bottom sheet dialog, your specified Activity is launched and created. The sent data (e.g. text, image, video and audio URI, etc) can be retrieved from the incoming Intent at onCreate() callback with different key value.

Before all start, it (line 7–8) is necessary to confirm the Intent is coming from either ACTION_SEND or ACTION_SEND_MULTIPLE instead of other Intent such as the implicit Intent used in startActivity(intent: Intent) function.

The data mime type should also be checked to ensure the correct key and function are used later to retrieve the sent data.

At 14–15, the sent text can be retrieved from the keyIntent.EXTRA_TEXT.

At 20–21, the sent image URI can be retrieved from the keyIntent.EXTRA_STREAM.

At 20–21, the URIs of sent multiple images can be retrieved from the keyIntent.EXTRA_STREAM but with another function getParcelableArrayListExtra(key: String).

Summary

  1. Activity must be declared with <intent-filter> to tell system that this Activity can handle certain system Intent with the specific action and data declared at the <action> and <data> tags.
  2. Either android.intent.action.SEND or android.intent.action.SEND_MULTIPLE should be declared at the <action> tag to receive data from another app.
  3. android.intent.category.DEFAULT must be defined at the <category> tag in order to show the app at the Sharing bottom sheet dialog.
  4. MIME type is declared at the <data> tag to tell system which kind of data this Activity can handle. Examples are text/*, image/*, audio/*, video/* and application/pdf.
  5. The app icon is shown with both app label and Activity label defined at the AndroidManifest.xml.
  6. Both IntentAction and data type have to be checked at the onCreate() callback of the specified Activity. Intent.EXTRA_TEXT is used to retrieve sent text and Intent.EXTRA_STREAM is used to retrieve sent image.

You are welcome to follow me at Twitter@myrick_chow for more information. Thank you for reading this article. Have a nice day! 😄

--

--

Mobile Lead @REAL Messenger Inc. https://real.co Focus on Android & iOS Native programming.