啓動並不一定是單向的另一個 Activity。您還可以啓動另一個 Activity 並 接收返回的結果。要接收結果,請調用 startActivityForResult()(而不是 startActivity())。
例如,您的應用可啓動相機應用並接收拍攝的照片作爲結果。或者,您可以啓動“聯繫人”應用以便用戶選擇聯繫人,並且您將接收聯繫人詳細信息作爲結果。
當然,響應的 Activity 必須設計爲返回結果。當它這樣做時,它會作爲另一 Intent 對象發送結果。您的 Activity 在 onActivityResult() 回調中接收它。
注:當您調用 startActivityForResult() 時,您可以使用明確或隱含 Intent。當啓動您自己的 Activity以接收結果時,您應使用明確(explicit) Intent 確保您可收到預期結果。
啓動 Activity
啓動要返回結果的 Activity 時,您所使用的 Intent 對象並沒有什麼特別之處,但您需要向 startActivityForResult() 方法傳遞額外的整數參數。
該整數參數是識別您的請求的“請求代碼”。當您收到結果Intent 時,回調提供相同的請求代碼,以便您的應用可以正確識別結果並確定如何處理它。
例如,此處顯示如何開始允許用戶選擇聯繫人的 Activity:
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);
}
接收結果
當用戶完成後續 Activity 並且返回時,系統會調用您的 Activity onActivityResult() 的方法。此方法包括三個參數:
- 您向 startActivityForResult() 傳遞的請求代碼。
- 第二個 Activity 指定的結果代碼。如果操作成功,這是 RESULT_OK;如果用戶退出或操作出於某種原因失敗,則是 RESULT_CANCELED。
- 傳送結果數據的 Intent。
本例說明您可以如何處理“選擇聯繫人” Intent 的結果。
@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)
}
}
}
在本例中, Android 的“聯繫人”應用返回的結果 Intent 提供識別用戶所選聯繫人的內容 Uri。
爲了成功處理結果,您必須瞭解結果的 Intent 的格式。當返回結果的 Activity 是您自己的 Activity 之一時,這便非常容易。 Android 平臺附帶的應用提供它們自己的 API,您可用這些 API 獲取特定結果數據。 例如,“聯繫人”應用始終返回帶內容 URI(識別所選聯繫人)的結果,並且“相機”應用在 “data” extra 中返回 Bitmap(請參閱有關拍攝照片的課程)。
獎勵:接收聯繫人數據
顯示如何從“聯繫人”應用獲取結果的代碼不會詳細說明如何實際從結果讀取數據,但它需要對內容提供商進行更深入的探討。 但是,如果您很好奇,此處提供了更多的代碼向您展示如何查詢結果數據,從所選聯繫人獲取電話號碼:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// Check which request it is that 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();
// 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 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, 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...
}
}
}
注:在 Android 2.3(API 級別 9)之前,在 Contacts Provider 上執行查詢(如上面所示)需要您的應用聲明READ_CONTACTS 權限。但是,自 Android 2.3 版本開始,“聯繫人”應用授予您的應用在聯繫人提供程序向您返回結果時從聯繫人提供程序臨時讀取信息的權限。該臨時權限僅適用於所請求的特定聯繫人,因此您只能查詢 Intent 的 Uri 指定的聯繫人,除非您聲明 READ_CONTACTS 權限。