(五)——通过全局变量传递数据

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、运行一下,得到结果:

文章导航