在Activity之間數據傳遞中還有一種比較實用的方式,就是全局對象,使用J2EE的讀者來說都知道Java Web的四個作用域,這四個作用域從小到大分別是:Page、Request、Session和Application,其中Application域在應用程序的任何地方都可以使用和訪問,除非是Web服務器停止,Android中的全局對象非常類似於Java Web中的Application域,除非是Android應用程序清除內存,否則全局對象將一直可以訪問。
下面通過例子來分享一下它的實現:
建立的工程結構如下:
MainActivity運行截圖:
OtherActivity運行截圖:
源代碼如下:
MyApp.java:
- package com.intent.activity;
- import android.app.Application;
- public class MyApp extends Application{
- public String name;
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- }
MainActivity.java:
- package com.intent.activity;
- import android.app.Activity;
- import android.content.Intent;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- public class MainActivity extends Activity implements OnClickListener{
- private MyApp myApp;
- private Button btn;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- btn = (Button)findViewById(R.id.btOpenOtherActivity);
- btn.setOnClickListener(this);
- }
- @Override
- public void onClick(View v) {
- myApp = (MyApp)getApplication();
- myApp.setName("wulianghuan");
- //定義一個意圖
- Intent intent = new Intent(MainActivity.this,OtherActivity.class);
- //啓動意圖
- startActivity(intent);
- }
- }
OtherActivity.java:
- package com.intent.activity;
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.TextView;
- public class OtherActivity extends Activity {
- private TextView text_name;
- private MyApp myApp;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.other);
- text_name = (TextView) findViewById(R.id.name);
- //獲取Intent傳遞的Bundle對象和它裏面的數據
- myApp = (MyApp)getApplication();
- //設置文本框的數據
- text_name.setText("姓名:"+myApp.getName());
- }
- }
main.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="這是:MainActivity" />
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:id="@+id/btOpenOtherActivity"
- android:text="使用Application傳遞數據"/>
- </LinearLayout>
注意:在AndroidManifest.xml需要對MyApp對象進行聲明:
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.intent.activity"
- android:versionCode="1"
- android:versionName="1.0" >
- <uses-sdk android:minSdkVersion="8" />
- <application
- android:name=".MyApp"
- android:icon="@drawable/ic_launcher"
- android:label="@string/app_name" >
- <activity
- android:label="@string/app_name"
- android:name=".MainActivity" >
- <intent-filter >
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- <activity android:name=".OtherActivity"/>
- </application>
- </manifest>