このエントリーをはてなブックマークに追加
>> 開発>> APIサンプル

App - Activity - Receive Result

Receive Result

別のActivityを開いて、そこでの選択結果を受け取ります。

Java

startActivityForResult(intent, GET_CODE); を使って、Sendコードを設定して、Resultを受け取るようにします。

Senderの方では、Resultコードを指定して、Intentに値を設定しています。 setResult(RESULT_OK, (new Intent()).setAction("Corky!"));

これを、Receiveの方の protected void onActivityResult(int requestCode, int resultCode, Intent data) 部分で受け取ります。

ReceiveResult.java

protected void onCreate(Bundle savedInstanceState) {
        // Be sure to call the super class.
        super.onCreate(savedInstanceState);

        // See assets/res/any/layout/hello_world.xml for this
        // view layout definition, which is being set here as
        // the content of our screen.
        setContentView(R.layout.receive_result);

        // Retrieve the TextView widget that will display results.
        mResults = (TextView)findViewById(R.id.results);

        // This allows us to later extend the text buffer.
        mResults.setText(mResults.getText(), TextView.BufferType.EDITABLE);

        // Watch for button clicks.
        Button getButton = (Button)findViewById(R.id.get);
        getButton.setOnClickListener(mGetListener);
    }

    /**
     * This method is called when the sending activity has finished, with the
     * result it supplied.
     * 
     * @param requestCode The original request code as given to
     *                    startActivity().
     * @param resultCode From sending activity as per setResult().
     * @param data From sending activity as per 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");
        }
    }

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

sendResult.java

protected void onCreate(Bundle savedInstanceState)
    {
        // Be sure to call the super class.
        super.onCreate(savedInstanceState);

    // See assets/res/any/layout/hello_world.xml for this
    // view layout definition, which is being set here as
    // the content of our screen.
    setContentView(R.layout.send_result);

    // Watch for button clicks.
    Button button = (Button)findViewById(R.id.corky);
    button.setOnClickListener(mCorkyListener);
    button = (Button)findViewById(R.id.violet);
    button.setOnClickListener(mVioletListener);
}

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

Layout

recieveresult
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:padding="4dip"
    android:gravity="center_horizontal"
    android:layout_width="fill_parent" android:layout_height="fill_parent">

    <TextView
        android:layout_width="fill_parent" android:layout_height="wrap_content"
        android:layout_weight="0"
        android:paddingBottom="4dip"
        android:text="@string/receive_result_instructions"/>

    <TextView android:id="@+id/results"
        android:layout_width="fill_parent" android:layout_height="10dip"
        android:layout_weight="1"
        android:paddingBottom="4dip"
        android:background="@drawable/green">
    </TextView>

    <Button android:id="@+id/get"
        android:layout_width="wrap_content" android:layout_height="wrap_content" 
        android:layout_weight="0"
        android:text="@string/receive_result_result">
        <requestFocus />
    </Button>

</LinearLayout>

sendresult
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:padding="4dip"
    android:gravity="center_horizontal"
    android:layout_width="fill_parent" android:layout_height="fill_parent">

    <TextView
        android:layout_width="fill_parent" android:layout_height="wrap_content"
        android:layout_weight="0"
        android:paddingBottom="8dip"
        android:text="@string/pick_result"/>

    <Button android:id="@+id/corky"
        android:layout_width="wrap_content" android:layout_height="wrap_content" 
        android:text="@string/corky">
        <requestFocus />
    </Button>

    <Button android:id="@+id/violet"
        android:layout_width="wrap_content" android:layout_height="wrap_content" 
        android:text="@string/violet">
    </Button>

</LinearLayout>

関連サイト
www.akjava.com | github.com/akjava
Copyright (C) 2008-20014 Aki Miyazakion Google+ All Rights Reserved.