1. 程式人生 > >Android學習筆記(五)——通過全域性變數傳遞資料

Android學習筆記(五)——通過全域性變數傳遞資料

1、全域性物件是Activity之間傳遞資料的一種比較實用的方式,比如在JavaWeb中有四個作用域,這四個作用域從小到大分別是Page、Request、Session和Application,其中Application域在應用程式的任何地方都可以使用和訪問,除非是Web伺服器停止。Android中的全域性物件非常類似於JavaWeb中的Application域,只要Android應用程式不清除記憶體,全域性物件就可以一直訪問~

2、新建一個Android專案:“android_app”,進入“main.xml”新增一個Button,程式碼如下:

    <Button
        android:id="@+id/button"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="使用Application傳遞資料" />
3、在當前目錄下建立一個“other.xml”,新增一個TextView,程式碼如下:
    <TextText
        android:id="@+id/msg"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >
    </TextText>
4、在當前包下新建一個類“OtherActivity”,並使其繼承Activity;新增一個“onCreate”方法,程式碼如下:
package com.android.app;

import android.app.Activity;
import android.os.Bundle;

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

5、為了能在程式中使用全域性變數,需要在當前包下新建一個普通類“MyApp”,使其繼承“Application”,隨後宣告一個“name”成員並提供構造方法和新增“onCreate”程式碼如下:

package com.android.app;

import android.app.Application;

public class MyApp extends Application {
	private String name;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		super.onCreate();
		setName("張三");
	}
}
6、進入“Main.java”,新增Button和MyApp成員,隨後為Button設定一個點選的事件,程式碼如下:
package com.android.app;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;

public class Main extends Activity {
	private Button button;
	private MyApp myApp;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		button = (Button) this.findViewById(R.id.button);
		// 為button註冊一個點選事件
		button.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				myApp = (MyApp) getApplication();
				myApp.setName("Jack");// 修改之後的名字
				Intent intent = new Intent(Main.this, OtherActivity.class);
				startActivity(intent);
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}
7、進入“OtherActivity.java”中,宣告一個MyApp成員,並獲取myApp,程式碼如下:
package com.android.app;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class OtherActivity extends Activity {
	private MyApp myApp;
	private TextView textView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.other);
		textView = (TextView) this.findViewById(R.id.msg);
		myApp = (MyApp) getApplication();
		textView.setText("-appname-->>" + myApp.getName());
	}
}
8、修改清單檔案“AndroidManifest.xml”,在“Application”標籤中新加入一個屬性“name”,再加入一個“Activity”,程式碼如下:
    <application
        android:name=".MyApp"//後新增的屬性
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.android.app.Main"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".OtherActivity" >//後新增的標籤
        </activity>
    </application>
9、執行一下,得到結果: