1. Code
  2. Android SDK

Android SDK: Receiving Data from the Send Intent

Read Time:8 minsLanguages:

On the Android platform, the Intent system allows users to share content between apps. You can send and receive multiple types of data on Android but in this tutorial we will implement receiving either a text String or an image, displaying the received content in the app's user interface!

Step 1: Start a New Android Project

Start by creating a new Android project in Eclipse. Choose "File", "New", "Project" - select "Android Application Project" and then click "Next". In the New Android Application window, enter your chosen Application, Project, and Package names and then choose "Next".

New Android ProjectNew Android ProjectNew Android Project

In the project configuration window, choose your options, letting Eclipse create an Activity for you. In the next screens, configure your launcher icon if appropriate, and then select "Blank Activity". In the Configure Activity screen, enter an Activity name and then choose "Finish".

Blank ActivityBlank ActivityBlank Activity

Eclipse should create Activity and layout files for your app.

Step 2: Edit the Project Manifest

In order for your app to appear in the chooser list presented when the user attempts to share data from another app, you need to alter the Project Manifest file. Open it in Eclipse and switch to the XML editor tab. Find the section containing your app's main Activity - you should see an Intent Filter element there already, indicating MAIN action and LAUNCHER category.

After the existing Intent Filter element, inside the main Activity element, add another Intent Filter:

This is where we define the Actions that can launch the app - the existing Intent Filter specifying the normal options for when the user launches from the homescreen or device application menu. By adding another Intent Filter element, we are specifying additional options for launching the app. Inside the new element, add an Action element specifying the SEND Intent Action:

This tells Android that when other apps launch a SEND Intent this app may receive what is being shared. Now give the Intent Filter a category:

Next we need to indicate the MIME types the app is going to receive:

This indicates that the app will receive any text or image type. You can optionally specify particular text or image types. There are lots of other types you can receive, including audio, video, message, and application. If you need your app to receive multiple data items of one of your MIME types, you can include the SEND_MULTIPLE Action as well as/instead of the SEND Action alone.

Here is the "DataReceiver" app appearing in the share list for the Gallery application, through which various image types can be shared.

The App in the Share ListThe App in the Share ListThe App in the Share List

Step 3: Define the App Layout

Let's design the app to display the received text String or image. Open the main layout file and switch to the XML tab to edit the code. Replace any existing code in the layout file with the following Relative Layout outline:

Alter the "tools:context" attribute if you chose a different Activity class name. Inside the layout, add the following to display a received image:

We will use the ID to refer to the Image View in our Java Activity, updating it to display any image received from the SEND Intent. Add the specified String to your app's "res/values/strings.xml" file:

Back in the layout file, add a Text View to display any text Strings received via the SEND Intent:

Again, we have an ID attribute we can use in the Java code.

Step 4: Receive the Intent

Open your app's Activity class file. Your class will need the following import statements, some of which may already be present:

Eclipse should have created a basic onCreate method applying your layout. After this line, retrieve the Views we want to update with any received data:

These use the ID attributes we included in the layout file. Now that we have a reference to the Views, we can update their appearance with data received from other applications. Within an Activity class, you can retrieve the Intent that was responsible for the Activity starting. Add the following line in your onCreate method:

Now we can use this to also retrieve the Action that was used when starting the Activity:

This will let us determine whether the app has launched from the device menu or from the share list. If the app has been launched with the SEND Action, there will also be a MIME type indicated within the Intent that started it. Retrieve this as follows:

If the app was launched, for example, from the main device menu rather than from a share list, this will be null. If it was launched with the SEND Action, we will be able to use this to handle the received data, tailoring the app response to the type of data received.

Users can share text from a variety of applications, including the Web browser - if a user chooses to share a Web page, this data is contained in a text String, so our app will be able to receive it:

The Browser MenuThe Browser MenuThe Browser Menu

Here is the app listed in the browser share list:

The App in the Browser Share ListThe App in the Browser Share ListThe App in the Browser Share List

Step 5: Handle Alternative Actions

Now we have two possibilities: the app has been launched from the device in the default fashion and is not receiving any incoming data; the app has been launched to share content, in which case there is either a text String or image incoming. Add a conditional statement to onCreate to handle these two scenarios:

If you look back at your Manifest file you will see that these are the Actions included in our two Intent Filter elements for the Activity. Let's complete the ACTION_MAIN else if statement first as it's simpler. In here you need to include whatever you want your app to do when nothing has been shared to it. For the purposes of this tutorial we will simply display a text String in the Text View:

Step 6: Determine MIME Type

Now to the ACTION_SEND if statement, in which we need to handle received data of the MIME types we listed in the Manifest. First add a conditional to handle the two types we listed, text and images:

If you set your app up to receive other types, simply add conditional statements here for each of them.

Step 7: Receive Shared Text Data

Let's receive text first. In the if statement for text data, first hide the Image View as we only need the Text View in this case:

Now retrieve the String as an Extra from the Intent:

We use the getStringExtra method for text data, passing the EXTRA_TEXT Intent constant to receive any character sequence passed using the SEND Action. Now let's make sure we have some data and display it in the app UI:

You can of course carry out further processing on the text if you need to - this is the text URL shared from the browser:

Shared Text in the App UIShared Text in the App UIShared Text in the App UI

Step 8: Receive Shared Image Data

Now let's turn to the else if statement for image data. As before, first set the alternative UI item to disappear as we don't need it:

Now we need to get the image URI, as this is what the Intent will be sharing:

This time we use the getParcelableExtra method - there are lots of other Intent methods for the different data types you can receive in your apps, including number types, arrays and lists. We pass the EXTRA_STREAM Intent constant this time, which is used to receive data other than text. Now we can use the image URI within the app - the sample code below simply displays it in the Image View but if you plan on receiving images in your own apps you should first resample the image data to avoid memory issues:

Tip: For guidance on avoiding memory problems when importing images see the Android Developers guide: Displaying Bitmaps Efficiently

Here is the image shared from the Gallery app:

Shared Image in the App UIShared Image in the App UIShared Image in the App UI


That's our basic receiver app complete. This is of course just the bare bones of a functional app in which data is received from the SEND Action, but the principle of setting your app up to appear in the share list and handling the incoming content remains. In general, it's worth thinking of the possible ways in which users may want to launch your apps, as the main menu launcher is really just the beginning.

Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.