W3Cschool
恭喜您成為首批注冊(cè)用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
編寫:kesenhoo - 原文:http://developer.android.com/training/basics/intents/result.html
啟動(dòng)另外一個(gè)activity并不一定是單向的。我們也可以啟動(dòng)另外一個(gè)activity然后接受一個(gè)返回的result。為接受result,我們需要使用startActivityForResult() ,而不是startActivity()。
例如,我們的app可以啟動(dòng)一個(gè)camera程序并接受拍的照片作為result?;蛘呖梢詥?dòng)聯(lián)系人程序并獲取其中聯(lián)系的人的詳情作為result。
當(dāng)然,被啟動(dòng)的activity需要指定返回的result。它需要把這個(gè)result作為另外一個(gè)intent對(duì)象返回,我們的activity需要在onActivityResult()的回調(diào)方法里面去接收result。
Note:在執(zhí)行
startActivityForResult()
時(shí),可以使用explicit 或者 implicit 的intent。當(dāng)啟動(dòng)另外一個(gè)位于的程序中的activity時(shí),我們應(yīng)該使用explicit intent來確??梢越邮盏狡诖慕Y(jié)果。
對(duì)于startActivityForResult() 方法中的intent與之前介紹的并無太大差異,不過是需要在這個(gè)方法里面多添加一個(gè)int類型的參數(shù)。
該integer參數(shù)稱為"request code",用于標(biāo)識(shí)請(qǐng)求。當(dāng)我們接收到result Intent時(shí),可從回調(diào)方法里面的參數(shù)去判斷這個(gè)result是否是我們想要的。
例如,下面是一個(gè)啟動(dòng)activity來選擇聯(lián)系人的例子:
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);
}
當(dāng)用戶完成了啟動(dòng)之后activity操作之后,系統(tǒng)會(huì)調(diào)用我們activity中的onActivityResult() 回調(diào)方法。該方法有三個(gè)參數(shù):
RESULT_OK
,如果用戶沒有操作成功,而是直接點(diǎn)擊回退或者其他什么原因,那么則是RESULT_CANCELED
例如,下面顯示了如何處理pick a contact的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)
}
}
}
本例中被返回的Intent使用Uri的形式來表示返回的聯(lián)系人。
為正確處理這些result,我們必須了解那些result intent的格式。對(duì)于自己程序里面的返回result是比較簡(jiǎn)單的。Apps都會(huì)有一些自己的api來指定特定的數(shù)據(jù)。例如,People app (Contacts app on some older versions) 總是返回一個(gè)URI來指定選擇的contact,Camera app 則是在data
數(shù)據(jù)區(qū)返回一個(gè) Bitmap (see the class about Capturing Photos).
上面的代碼展示了如何獲取聯(lián)系人的返回結(jié)果,但沒有說清楚如何從結(jié)果中讀取數(shù)據(jù),因?yàn)檫@需要更多關(guān)于content providers的知識(shí)。但如果想知道的話,下面是一段代碼,展示如何從被選的聯(lián)系人中讀出電話號(hào)碼。
@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...
}
}
}
Note:在Android 2.3 (API level 9)之前對(duì)
Contacts Provider
的請(qǐng)求(比如上面的代碼),需要聲明READ_CONTACTS
權(quán)限(更多詳見Security and Permissions)。但如果是Android 2.3以上的系統(tǒng)就不需要這么做。但這種臨時(shí)權(quán)限也僅限于特定的請(qǐng)求,所以仍無法獲取除返回的Intent以外的聯(lián)系人信息,除非聲明了READ_CONTACTS
權(quán)限。
Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: