Android Get Result From Other App

Get Result From Other App

A - Preface

In previous post, I show you how to use Implicit Intent to call another app.

But calling another app doesn’t have only one-way. You can also can recieve the result from calling app.

Some scenerios that you need to use this technique:

  • Start the camera app and get the photo as a result
  • Start the contacts app and get the contact info as result

That’s the main content of this post also - Get Result From Other App.

B - Start another app

Please prefer this post for details

C - Get Result From Other App

To get result from other app, we have to implement the onActivityResult() method. This method include 3 args:

  • requestCode: the code you use to call other app

  • resultCode: use to specify the result back

    • RESULT_OK: successful operation
    • RESULT_CANCELED: failed operation or user backed out
  • data: the Intent that contain the result data

D - Demo Read contact data

Firstly, create a simple layout for the demo app

<RelativeLayout xmlns:android=""
    android:layout_height="fill_parent" >

        android:text="Pick Contact" />

        android:textAppearance="?android:attr/textAppearanceLarge" />


Then, implemnt on click event for btnPickContact to start People app:

btnPickContact.setOnClickListener(new OnClickListener() {
	public void onClick(View arg0) {
		Intent intent = new Intent(Intent.ACTION_PICK,  ContactsContract.Contacts.CONTENT_URI);
	        startActivityForResult(intent, PICK_CONTACT_REQUEST);

Finally, handle the Intent result to have desired data:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
	// Check which request we're responding to
	if (requestCode == PICK_CONTACT_REQUEST) {
		// Make sure the request was successful
		if (resultCode == RESULT_OK) {
			// Get the URI that points to the selected contact
			Uri contactUri = data.getData();

			// Perform the query on the contact to get the NUMBER column
			// We don't need a selection or sort order (there's only one result for the given URI)
			// CAUTION: The query() method should be called from a separate thread to avoid blocking
			// your app's UI thread. (For simplicity of the sample, this code doesn't do that.)
			// Consider using CursorLoader to perform the query.
			Cursor cursor = getContentResolver()
					.query(contactUri, null, null, null, null);

			// Retrieve the contact display name from the DISPLAY_NAME column
			int columnName = cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
			String name = cursor.getString(columnName);

			// Display the result on the TextView

After run the demo app, you can have the result like below:


E - Download Souce Code for Get Result From Other App demo

This post is licensed under CC BY 4.0 by the author.