Android Receiving Data from another app in 2 steps
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.
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 :
- <intent-filter>
Declare that thisActivity
is applicable to handle systemIntent
with theaction
anddata
defined at the<action>
and<data>
tags respectively. - <action>
Define theIntentAction
that thisActivity
can handle, i.e.android.intent.action.SEND
to receive a single datum andandroid.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 requestedIntent
. - <category>
android.intent.category.DEFAULT
must be defined at the<category>
else theActivity
cannot receive any implicitIntent
and therefore cannot be shown at the Sharing bottom sheet dialog! - <data>
Declare the type of MIME (Multipurpose Internet Mail Extensions) that thisActivity
can take care. There are several common types:
1.text/*
which includestext/plain
,text/rtf
,text/html
,text/json
2.image/*
which includesimage/jpg
,image/png
,image/gif
3.video/*
which includesvideo/mp4
,video/3gp
4.audio/*
which includesaudio/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 URI
s of sent multiple images can be retrieved from the keyIntent.EXTRA_STREAM
but with another function getParcelableArrayListExtra(key: String)
.
Summary
Activity
must be declared with<intent-filter>
to tell system that thisActivity
can handle certain systemIntent
with the specificaction
anddata
declared at the<action>
and<data>
tags.- Either
android.intent.action.SEND
orandroid.intent.action.SEND_MULTIPLE
should be declared at the<action>
tag to receive data from another app. android.intent.category.DEFAULT
must be defined at the<category>
tag in order to show the app at the Sharing bottom sheet dialog.- MIME type is declared at the
<data>
tag to tell system which kind of data thisActivity
can handle. Examples aretext/*
,image/*
,audio/*
,video/*
andapplication/pdf
. - The app icon is shown with both app label and
Activity
label defined at the AndroidManifest.xml. - Both
IntentAction
and data type have to be checked at theonCreate()
callback of the specifiedActivity
.Intent.EXTRA_TEXT
is used to retrieve sent text andIntent.EXTRA_STREAM
is used to retrieve sent image.
You would probably like my other Intent
related post:
You are welcome to follow me at Twitter@myrick_chow for more information. Thank you for reading this article. Have a nice day! 😄