Introduction
Getting the result from an activity
Start an activity
- //static final int PICK_CONTACT_REQUEST = 1; // The request code
- //...
- static final int PICK_CONTACT_REQUEST = 1; // The request code
- ...
- private void pickContact() {
- Intent pickContactIntent = new Intent(Intent.ACTION_PICK, Uri.parse("content://contacts"));
- pickContactIntent.setType(Phone.CONTENT_TYPE); // Show user only contacts w/ phone numbers
- startActivityForResult(pickContactIntent, PICK_CONTACT_REQUEST);
- }
Receive the Result
- @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) {
- // The user picked a contact.
- // The Intent's data Uri identifies which contact was selected.
- // Do something with the contact here (bigger example below)
- }
- }
- }
- @Override
- protected void onActivityResult(int requestCode, int resultCode, Intent data) {
- // Check which request it is
- 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
- String[] projection = {Phone.NUMBER};
- // Perform the query on the contact to get the NUMBER column
- // The query() method should be called from a separate thread to avoid blocking
- // your app's UI thread. (For the sake of simplicity of the sample, this code doesn't do that.)
- // Consider using CursorLoader to perform the query.
- Cursor cursor = getContentResolver()
- .query(contactUri, projection, null, null, 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 Android 2.3, in other words API level 9, performing a query on the contacts, the provider requires READ_CONTACTS permission for security concerns.
Allowing Another Application to Start our Activity
Add an Intent Filter
- Action
A string naming the action to perform is usually one of the platform's defined values, such as ACTION_SEND or ACTION_VIEW. Specify this in your intent filter with the <action> element. The value you specify in this element must be the full string name for the action, instead of the API constant (see the examples below).
- DataA description of the data associated with the intent.Specify this in your intent filter with the <data> element. Using one or more attributes in this element, you can specify just the MIME type, just a URI prefix, just a URI scheme, or a combination of these and others that indicate the data type accepted.
- Category
It provides an additional way to characterize the activity handling the intent, usually related to the user gesture or location from which it's started. There are several different categories supported by the system, but most are rarely used. However, all implicit intents are defined with CATEGORY_DEFAULT by default.
- <activity android:name="ShareActivity">
- <intent-filter>
- <action android:name="android.intent.action.SEND"/>
- <category android:name="android.intent.category.DEFAULT"/>
- <data android:mimeType="text/plain"/>
- <data android:mimeType="image/*"/>
- </intent-filter>
- </activity>
- <!-- filter for sending text; accepts SENDTO action with sms URI schemes -->
- <intent-filter>
- <action android:name="android.intent.action.SENDTO"/>
- <category android:name="android.intent.category.DEFAULT"/>
- <data android:scheme="sms" />
- <data android:scheme="smsto" />
- </intent-filter>
- <!-- filter for sending text or images; accepts SEND action and text or image data -->
- <intent-filter>
- <action android:name="android.intent.action.SEND"/>
- <category android:name="android.intent.category.DEFAULT"/>
- <data android:mimeType="image/*"/>
- <data android:mimeType="text/plain"/>
- </intent-filter>
- </activity>
Now to receive the implicit intents you must include category_Default in the intent filter.
Handle the Intent in Activity
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- // Get the intent that started this activity
- Intent intent = getIntent();
- Uri data = intent.getData();
- // Figure out what to do based on the intent type
- if (intent.getType().indexOf("image/") != -1) {
- // Handle intents with image data ...
- } else if (intent.getType().equals("text/plain")) {
- // Handle intents with text ...
- }
- }
Returning a Result
- Intent result = new Intent("com.example.RESULT_ACTION", Uri.parse("content://result_uri");
- setResult(Activity.RESULT_OK, result);
- finish();
- setResult(RESULT_COLOR_RED);
- finish();

Neeraj KumarPosted Jul 30, 2015, 4:36 PM
Nice
Neeraj KumarPosted Jul 30, 2015, 4:30 PM
Nice Article
Sibeesh VenuPosted Feb 23, 2015, 7:25 AM
Good One.