Android Get Result From Other App
Post
Cancel

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="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <Button
        android:id="@+id/btnPickContact"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:text="Pick Contact" />

    <TextView
        android:id="@+id/tvContact"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/btnPickContact"
        android:text="Contact"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>

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

btnPickContact.setOnClickListener(new OnClickListener() {
	@Override
	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:

@Override
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);
			cursor.moveToFirst();

			// 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
			tvContact.setText(name);
		}
	}
}

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

demo_recieve_réult_from_other_app[1]

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

 https://drive.google.com/file/d/0Bw3dwdSezn6fN241a3AyRDd1bGc/edit?usp=sharing

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