Android API Demos學習(4) - Receive Result

來源:互聯網
上載者:User

本例展示一個Activity怎麼接受通過它開啟的另外一個Activity返回的結果。


比較常見的例子是,傳送簡訊的時候,需要開啟連絡人程式選擇連絡人,然後返回選擇的資訊到發送程式。


setResult()方法負責發送資訊,onActivityResult()方法負責接受資訊。
// Definition of the one requestCode we use for receiving resuls.
    static final private int GET_CODE = 0;


    private OnClickListener mGetListener = new OnClickListener() {
        public void onClick(View v) {
            // Start the activity whose result we want to retrieve.  The
            // result will come back with request code GET_CODE.
            Intent intent = new Intent(ReceiveResult.this, SendResult.class);
            startActivityForResult(intent, GET_CODE);
        }
    };

 

startActivityForResult方法啟動我們想從那裡取得資訊的Activity,GET_CODE會在返回資訊的時候一起返回,那樣我們就通過GET_CODE出來返回的結果。

www.2cto.com
在SendResult.java中:
private OnClickListener mCorkyListener = new OnClickListener()
    {
        public void onClick(View v)
        {
            // To send a result, simply call setResult() before your
            // activity is finished.
            setResult(RESULT_OK, (new Intent()).setAction("Corky!"));
            finish();
        }
    };


    private OnClickListener mVioletListener = new OnClickListener()
    {
        public void onClick(View v)
        {
            // To send a result, simply call setResult() before your
            // activity is finished.
            setResult(RESULT_OK, (new Intent()).setAction("Violet!"));
            finish();
        }
    };


在程式停止前調用setResult方法返回結果。
@Override
protected void onActivityResult(int requestCode, int resultCode,
Intent data) {
        // You can use the requestCode to select between multiple child
        // activities you may have started.  Here there is only one thing
        // we launch.
        if (requestCode == GET_CODE) {


            // We will be adding to our text.
            Editable text = (Editable)mResults.getText();


            // This is a standard resultCode that is sent back if the
            // activity doesn't supply an explicit result.  It will also
            // be returned if the activity failed to launch.
            if (resultCode == RESULT_CANCELED) {
                text.append("(cancelled)");


            // Our protocol with the sending activity is that it will send
            // text in 'data' as its result.
            } else {
                text.append("(okay ");
                text.append(Integer.toString(resultCode));
                text.append(") ");
                if (data != null) {
                    text.append(data.getAction());
                }
            }


            text.append("\n");
        }
    }

 

onActivityResult方法接受返回的結果,在onResume方法前執行。三個參數的意思分別是:
1. requestCode就是startActivityForResult中的第二個參數,可以用來區分是哪一個activity發送的請求。
2. resultCode是子activity返回的code,一般為RESULT_OK和RESULT_CANCELLED。
3. data是返回的結果資料。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.