Android接收Activity返回的結(jié)果

2018-08-02 17:27 更新

編寫: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é)果。

啟動(dòng)Activity

對(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);
}

接收Result

當(dāng)用戶完成了啟動(dòng)之后activity操作之后,系統(tǒng)會(huì)調(diào)用我們activity中的onActivityResult() 回調(diào)方法。該方法有三個(gè)參數(shù):

  • 通過startActivityForResult()傳遞的request code。
  • 第二個(gè)activity指定的result code。如果操作成功則是RESULT_OK ,如果用戶沒有操作成功,而是直接點(diǎn)擊回退或者其他什么原因,那么則是RESULT_CANCELED
  • 包含了所返回result數(shù)據(jù)的intent。

例如,下面顯示了如何處理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)系人數(shù)據(jù)

上面的代碼展示了如何獲取聯(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)限。


以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)