Android-get results from activity

Source: Internet
Author: User

This article translated from: http://developer.android.com/training/basics/intents/result.html

Starting another activity is not one-way. You can start another activity and accept the results returned by the started activity. You need to call startactivityforresult () instead of startactivity () method.

For example, your app can start a camera app and receive the photos taken by the app as a result. Or to allow users to select contacts, you can start the people application and receive the contact details as a result.

Of course, the activity that responds to the request must be designed to return a result. When a result is returned, it sends the result as an intent object. Your activity receives this object in the onactivityresult () callback method.

Note: when calling the startactivityforresult () method, you can use a clear or implicit intent object. When the activity you want to start is your own defined activity, you should use a clear intent object to ensure that you can receive the expected results.

Start Activity

There are no special requirements on the intent object used to start the activity that can return results, but you need to pass an additional integer parameter to the startactivityforresult () method.

This integer parameter is a request code that identifies your request. When you receive an intent object, the callback method provides the same request code so that your application can correctly identify the result and determine how to handle it.

For example, the following code starts an activity that allows users to select a contact:

Staticfinalint pick_contact_request
= 1; // The Request Code
...
Private void pickcontact (){
Intent pickcontactintent = new intent (intent. action_pick, new uri ("content: // contacts "));
Pickcontactintent. settype (phone. content_type); // show user only contacts w/phone numbers
Startactivityforresult (pickcontactintent, pick_contact_request );
}

Receive results

When you use the started activity and return it, the system will call the onactivityresult () method of your activity. This method includes three parameters:

1. The request code you pass to the startactivityforresult () method;

2. The result code specified by the second activity. If the operation succeeds, it is result_ OK. If the user exits or fails the operation for some reason, it is result_canceled.

3. An intent object carrying the result data.

For example, the following code processes the intent object of the selected contact:

@ Override
Protected void onactivityresult (INT requestcode, int resultcode, intent data ){
// Check which request we're re responding
If (requestcode = pick_contact_request ){
// Make sure the request was successful
If (resultcode = result_ OK ){
// The user picked a contact.
// The intent's data URI identifies which contact was selected.
 
// Do something with the contact here (bigger example below)
}
}
}

In this example, the intent object returned from the contacts or people application of Android provides a content URI that identifies the selected contact.

To successfully process the returned results, you must understand the format of the returned intent object. When the returned result is provided by your own activity, it is easy to do this. Android applications provide their own APIs to obtain specific result data from the returned intent object. For example, the people application (the contacts application in some older versions) always returns the content URI that identifies the selected contact, in addition, the camera application will return a bitmap object in the "data" additional field.

Read contact data

The code above describes how to obtain the returned results from the people application, but does not detail how to read data from the returned results, because it needs to discuss some more advanced content
Providers topic. The following code queries the result data to obtain the phone number of the selected contact:

@ Override
Protected void onactivityresult (INT requestcode, int resultcode, intent data ){
// Check which request it is that we're re responding
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 ();
// We only need the number column, because there will be only one row in the result
String [] projection = {Phone. Number };
 
// 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 shocould 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 usingCursorLoaderTo perform the query.
Cursor cursor = getcontentresolver ()
. Query (contacturi, projection, null );
Cursor. movetofirst ();
 
// Retrieve the phone number from the Number Column
Int column = cursor. getcolumnindex (phone. number );
String number = cursor. getstring (column );
 
// Do something with the phone number...
}
}
}

Note: Before android2.3 (API level 9), execute a query based on the contacts provider and require your application to declare the read_contacts permission. However, since android2.3, the contacts/People application grants a temporary permission to your application so that it can obtain the returned data from the contacts provider. This Temporary Permission applies only to the contact of a specific request. Therefore, you cannot use this returned result to query data other than the contact specified by the intent Uri, unless you have declared the read_contacts permission.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.