1. 程式人生 > >Intent的用法(一),啟動activity傳遞資料以及startActivityForResult

Intent的用法(一),啟動activity傳遞資料以及startActivityForResult

Intent很神奇.可以用Intent來啟動新的Activity,啟動廣播,啟動服務,傳送資料........太多了.

這裡介紹下,使用Intent啟動新的Activity,傳遞資料,以及startActivityForResult()方法的使用.

我們這裡有兩個Activity,MainActivity.java和OtherActivity.java. 我們需要做的是,點選Button的時候,獲取到MainActivity中的使用者輸入的資料,傳遞給OtherActivity,在OtherActivity上進行整合後,在傳遞給MainActivity進行顯示.

先看效果如,也就是整個流程:


第一步  從MainActivity跳轉到OtherActivity,並將輸入的資料傳遞過去.

程式碼如下:

			// 將Intent初始化 Intent(packageContext, cls)
			// packageContext指的是當前Activity
			// cls指的是目標Activity
			intent = new Intent(MainActivity.this, OtherActivity.class);
			// 建立Bundle物件用來存放資料,Bundle物件可以理解為資料的載體
			Bundle b = new Bundle();
			// 呼叫Bundle物件的putString方法,採用 key-value的形式儲存資料
			b.putString("name", name.getText().toString());
			b.putString("age", age.getText().toString());
			// 將資料載體BUndle物件放入Intent物件中.
			intent.putExtras(b);
			// 呼叫startActivityForResult方法
			// startActivityForResult(intent,requestCode);
			// intent,資料載體
			// requestCode 請求的Code,這裡一般 大於等於0的整型資料就可以.
			startActivityForResult(intent, 1);

註釋已經比較詳細了,這裡值得一提的是,由於需要OtherActivity返回資料,所以採用了startActivityForResult()的方法,如果不需要返回資料,而是單純的啟動Activity,只需要使用tartActivity();就可以了.

第二步,OtherActivity接收資料,核心程式碼如下:

		// 獲取資料
		mIntent = getIntent();
		Bundle b = mIntent.getExtras();
		// 載入到tv
		tv.setText("輸入的姓名是:" + b.getString("name") + "輸入的年齡是:"
				+ b.getString("age"));
這裡建立一個Bundle物件後,呼叫getString方法,根據先前設定的key而獲取到資料.

第三步,返回資料,這一步可以分為兩個部分:

       OtherActivity將需要返回的資料封裝,程式碼如下:

			mIntent = new Intent(OtherActivity.this, MainActivity.class);
			Bundle b = new Bundle();
			b.putString("data", tv.getText().toString());
			mIntent.putExtras(b);
			this.setResult(RESULT_OK, mIntent);
			OtherActivity.this.finish();

      這裡的setResult方法setResult(resultCode, data); resultCode,我們採用了系統提供的RESULT_OK,它的作用是用來識別Intent的.具體作用,在MainActivity的程式碼看完後,就明白了.還需要指出的是,setResult()之後,一定要呼叫當前Activity的finish()方法,如果不呼叫finish()方法,回報什麼錯呢?其實最好是親自試一下.如果不finish(),點選"返回"按鈕的時候,並沒有執行跳轉.這一點很重要.

     MainActivity需要進行的處理,接收到資料後,將資料顯示在TextView中,程式碼如下:

	// 需要接收上一個Activity返回的資料,要重寫Activity的 onActivityResult()方法.
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		// 依據resultCode進行接收,這也是上個介面中需要設定resultCode的原因了
		switch (resultCode) {
		case RESULT_OK:
			// 這裡寫的比較省略,完整的寫法應該是:
			// Bundle b = data.getExtras();
			// String show_string = b.getString("data");
			// show.setText(show_string);
			show.setText(data.getExtras().getString("data"));
			break;

		default:
			break;
		}
		super.onActivityResult(requestCode, resultCode, data);
	}

以上是各個模組的核心程式碼.

接下來貼出來所有檔案的原始碼.

兩個xml檔案:

mian.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

    <EditText
        android:id="@+id/name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="輸入姓名" >

        <requestFocus />
    </EditText>

    <EditText
        android:id="@+id/age"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="輸入年齡" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="啟動Intent,傳遞資料,接收資料" />

    <TextView
        android:id="@+id/show"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="顯示整合好的資料"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>

other.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linearLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="返回" />

</LinearLayout>

兩個Activity的檔案:

MainActivity.java

package com.yongchun.intent.ui;

/*
 *     public Bundle() {
 *       mMap = new HashMap<String, Object>();
 *      mClassLoader = getClass().getClassLoader();
 *      }
 *      Bundle的構造方法中,初始化了一個Map,實際上Bundle就是Map的進一步的封裝,
 *      Bundle實際上採用的就是Map來儲存資料
 *      
 *   #####################################################   
 *      
 *     public Intent putExtra(String name, boolean value) {
 *          if (mExtras == null) {
 *             mExtras = new Bundle();
 *        }
 *       mExtras.putBoolean(name, value);
 *      return this;
 *     }
 *     
 *     Intent的putExtra方法中,實際上也是建立了一個Bundle物件.
 *     兩種方式是一樣的,換湯不換藥.
 * */
import java.util.HashMap;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.style.UpdateLayout;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener {
	/** Called when the activity is first created. */

	private Button startButton;
	private EditText name, age;
	private TextView show;
	private Intent intent;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		// 獲取引用
		startButton = (Button) findViewById(R.id.button1);
		startButton.setOnClickListener(this);
		name = (EditText) findViewById(R.id.name);
		age = (EditText) findViewById(R.id.age);
		show = (TextView) findViewById(R.id.show);

	}

	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.button1:

			// 將Intent初始化 Intent(packageContext, cls)
			// packageContext指的是當前Activity
			// cls指的是目標Activity
			intent = new Intent(MainActivity.this, OtherActivity.class);
			// 建立Bundle物件用來存放資料,Bundle物件可以理解為資料的載體
			Bundle b = new Bundle();
			// 呼叫Bundle物件的putString方法,採用 key-value的形式儲存資料
			b.putString("name", name.getText().toString());
			b.putString("age", age.getText().toString());
			// 將資料載體BUndle物件放入Intent物件中.
			intent.putExtras(b);
			// 呼叫startActivityForResult方法
			// startActivityForResult(intent,requestCode);
			// intent,資料載體
			// requestCode 請求的Code,這裡一般 大於等於0的整型資料就可以.
			startActivityForResult(intent, 1);

			break;

		default:
			break;
		}

	}

	// 需要接收上一個Activity返回的資料,要重寫Activity的 onActivityResult()方法.
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		// 依據resultCode進行接收,這也是上個介面中需要設定resultCode的原因了
		switch (resultCode) {
		case RESULT_OK:
			// 這裡寫的比較省略,完整的寫法應該是:
			// Bundle b = data.getExtras();
			// String show_string = b.getString("data");
			// show.setText(show_string);
			show.setText(data.getExtras().getString("data"));
			break;

		default:
			break;
		}
		super.onActivityResult(requestCode, resultCode, data);
	}

}

OtherActivity.java
package com.yongchun.intent.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

/**
 * 作者:肥魚 QQ群:104780991 Email:[email protected]
 * 關於:一條致力於Android開源事業的魚,還是肥的.吃得多賺的少還不會暖床,求包養.
 */
public class OtherActivity extends Activity implements OnClickListener {

	private TextView tv;
	private Button OK;
	private Intent mIntent;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.other);

		// 獲取引用
		tv = (TextView) findViewById(R.id.textView1);
		OK = (Button) findViewById(R.id.button1);
		OK.setOnClickListener(this);
		// 獲取資料
		mIntent = getIntent();
		Bundle b = mIntent.getExtras();
		// 載入到tv
		tv.setText("輸入的姓名是:" + b.getString("name") + "輸入的年齡是:"
				+ b.getString("age"));
	}

	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.button1:
			mIntent = new Intent(OtherActivity.this, MainActivity.class);
			Bundle b = new Bundle();
			b.putString("data", tv.getText().toString());
			mIntent.putExtras(b);
			this.setResult(RESULT_OK, mIntent);
			OtherActivity.this.finish();
			break;

		default:
			break;
		}

	}

}

在MainActivity.java的開頭處有一大段註釋,說的是Bundle儲存資料的機制以及使用Intent物件的putString方法儲存資料的機制.Bundle實際上就是對Map的進一步的封裝.而是用Intent物件的putString方法時,實際上還是建立了一個Bundle物件.我們來看一下Bundle類中的建構函式:

    public Bundle() {
        mMap = new HashMap<String, Object>();
        mClassLoader = getClass().getClassLoader();
    }

很明顯,實際上就是初始化了HashMap.

我們再看一下Intent類中某個putExtra()的方法.

    public Intent putExtra(String name, char value) {
        if (mExtras == null) {
            mExtras = new Bundle();
        }
        mExtras.putChar(name, value);
        return this;
    }

我們看到,實際上還是new了一個Bundle出來.

所以說,兩種方式實際上是一樣的,在效率上應該也是一樣的.不過這是肥魚自己的看法,如有不對,還請大家指正.

對於Intent的用法,今天暫時就寫到這裡.

插播廣告: QQ群:   104780991   歡迎致力於Android開源事業的朋友.