Android入門篇四:使用全局變量在Activity之間傳遞數據

 在Activity之間數據傳遞中還有一種比較實用的方式,就是全局對象,使用J2EE的讀者來說都知道Java Web的四個作用域,這四個作用域從小到大分別是:Page、Request、Session和Application,其中Application域在應用程序的任何地方都可以使用和訪問,除非是Web服務器停止,Android中的全局對象非常類似於Java Web中的Application域,除非是Android應用程序清除內存,否則全局對象將一直可以訪問。

下面通過例子來分享一下它的實現:

建立的工程結構如下:

MainActivity運行截圖:

OtherActivity運行截圖:

 

源代碼如下:

MyApp.java:

 

  1. package com.intent.activity;  
  2.   
  3. import android.app.Application;  
  4.   
  5. public class MyApp extends Application{  
  6.     public String name;  
  7.   
  8.     public String getName() {  
  9.         return name;  
  10.     }  
  11.   
  12.     public void setName(String name) {  
  13.         this.name = name;  
  14.     }     
  15.       
  16. }  


MainActivity.java:

 

 

  1. package com.intent.activity;  
  2.   
  3. import android.app.Activity;  
  4. import android.content.Intent;  
  5. import android.os.Bundle;  
  6. import android.view.View;  
  7. import android.view.View.OnClickListener;  
  8. import android.widget.Button;  
  9.   
  10. public class MainActivity extends Activity implements OnClickListener{  
  11.     private MyApp myApp;  
  12.     private Button btn;  
  13.     @Override  
  14.     public void onCreate(Bundle savedInstanceState) {  
  15.         super.onCreate(savedInstanceState);  
  16.         setContentView(R.layout.main);  
  17.         btn = (Button)findViewById(R.id.btOpenOtherActivity);  
  18.         btn.setOnClickListener(this);  
  19.     }  
  20.     @Override  
  21.     public void onClick(View v) {  
  22.         myApp = (MyApp)getApplication();  
  23.         myApp.setName("wulianghuan");  
  24.         //定義一個意圖  
  25.         Intent intent = new Intent(MainActivity.this,OtherActivity.class);  
  26.         //啓動意圖  
  27.         startActivity(intent);  
  28.     }  
  29. }  


OtherActivity.java:

 

 

  1. package com.intent.activity;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.view.View;  
  6. import android.view.View.OnClickListener;  
  7. import android.widget.Button;  
  8. import android.widget.TextView;  
  9.   
  10. public class OtherActivity extends Activity {  
  11.     private TextView text_name;  
  12.     private MyApp myApp;  
  13.   
  14.     @Override  
  15.     public void onCreate(Bundle savedInstanceState) {  
  16.         super.onCreate(savedInstanceState);  
  17.         setContentView(R.layout.other);  
  18.         text_name = (TextView) findViewById(R.id.name);  
  19.         //獲取Intent傳遞的Bundle對象和它裏面的數據  
  20.         myApp = (MyApp)getApplication();  
  21.         //設置文本框的數據  
  22.         text_name.setText("姓名:"+myApp.getName());  
  23.   
  24.     }  
  25. }  


main.xml:

 

 

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <TextView  
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="wrap_content"  
  10.         android:text="這是:MainActivity" />  
  11.       
  12.     <Button   
  13.         android:layout_width="wrap_content"  
  14.         android:layout_height="wrap_content"  
  15.         android:id="@+id/btOpenOtherActivity"  
  16.         android:text="使用Application傳遞數據"/>  
  17.   
  18. </LinearLayout>  


注意:在AndroidManifest.xml需要對MyApp對象進行聲明:

 

 

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     package="com.intent.activity"  
  4.     android:versionCode="1"  
  5.     android:versionName="1.0" >  
  6.   
  7.     <uses-sdk android:minSdkVersion="8" />  
  8.   
  9.     <application  
  10.         android:name=".MyApp"  
  11.         android:icon="@drawable/ic_launcher"  
  12.         android:label="@string/app_name" >  
  13.         <activity  
  14.             android:label="@string/app_name"  
  15.             android:name=".MainActivity" >  
  16.             <intent-filter >  
  17.                 <action android:name="android.intent.action.MAIN" />  
  18.                 <category android:name="android.intent.category.LAUNCHER" />  
  19.             </intent-filter>  
  20.         </activity>  
  21.         <activity android:name=".OtherActivity"/>  
  22.     </application>  
  23.   
  24. </manifest>  
  25.  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章