Android—擷取來自Activity的結果

來源:互聯網
上載者:User

本文譯自:http://developer.android.com/training/basics/intents/result.html

啟動另外一個Activity並不都是單向的,你也可以啟動另外一個Activity,並接受被啟動的Activity所返回的結果,這需要調用startActivityForResult()方法,而不是startActivity()方法。

例如,你的應用程式可以啟動一個Camera應用程式,並把該應用程式拍攝的照片作為結果來接收。或者為了讓使用者選擇連絡人,你可以啟動People應用程式,並且把連絡人細節作為結果來接收。

當然,響應請求的Activity必須要比設計成能夠返回一個結果。在返回結果時,它會把結果作為一個Intent對象來發送。你的Activity在onActivityResult()回調方法中接收這個對象。

注意:在調用startActivityForResult()方法時,你可以使用明確的或隱含的Intent對象。當你要啟動的Activity是你自己的定義Activity時,你應該使用明確的Intent對象,以便確保你能夠接收到期望的結果。

啟動Activity

有關用於能夠啟動返回結果的Activity的Intent對象,沒有特殊的要求,但是你需要把一個附加的整數參數傳遞給startActivityForResult()方法。

這個整數參數是一個標識你的請求的請求代碼。當你接收到Intent對象時,回調方法會提供相同的請求代碼,以便你的應用程式能夠正確識別結果,並判定如何處理它。

例如,以下代碼啟動了一個允許使用者選擇連絡人的Activity:

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

接收結果

當使用者使用完被啟動的Activity並返回時,系統會調用你的Activity的onActivityResult()方法,這個方法包括三個參數:

1. 你傳遞給startActivityForResult()方法的請求代碼;

2. 由第二個Activity指定的結果代碼。如果操作成功,它是RESULT_OK,如果使用者退出或因為某些原因讓操作失敗,則是RESULT_CANCELED.

3. 一個攜帶結果資料的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的Contacts或People應用程式中返回的Intent對象提供了一個標識使用者選擇的連絡人的內容Uri。

為了能夠成功的處理返回結果,你必須瞭解被返回的Intent對象的格式。當返回的結果是有你自己的Activity提供的時候,做這件事就很容易。Android平台的應用程式都提供了它們自己的API來從返回的Intent對象中擷取具體的結果資料。例如,People應用程式(某些較舊版本上的Contacts應用程式)總是返回標識使用者選擇的連絡人的內容URI,並且Camera應用會在“data”附加欄位中返回一個Bitmap對象。

讀取連絡人資料

以上代碼介紹了如何擷取來自People應用程式的返回結果,但沒有詳細的介紹如何從返回結果中讀取資料,因為它需要討論一些更進階的有關content
Providers的話題。以下代碼會告訴你如何查詢結果資料,從而擷取使用者選擇的連絡人的電話號碼:

@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... 
        } 
    } 
}

注意:在Android2.3(API Level 9)之前,執行基於Contacts提供器的查詢,會要求你的應用程式聲明READ_CONTACTS許可權。但是,從Android2.3開始,Contacts/People應用程式會給你的應用程式授予一個臨時許可權,以便能夠從Contacts提供器中擷取被返回的資料。這個臨時許可權僅適用於具體的請求的連絡人,因此你不能使用這個返回結果來查詢由Intent的Uri指定的連絡人以外的資料,除非你聲明了READ_CONTACTS許可權。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.