在Activity之间数据传递中还有一种比较实用的方式,就是全局对象,使用J2EE的读者来说都知道Java Web的四个作用域,这四个作用域从小到大分别是:Page、Request、Session和Application,其中Application域在应用程序的任何地方都可以使用和访问,除非是Web服务器停止,Android中的全局对象非常类似于Java Web中的Application域,除非是Android应用程序清除内存,否则全局对象将一直可以访问。

下面通过例子来分享一下它的实现:

建立的工程结构如下:

MainActivity运行截图:

OtherActivity运行截图:

 

源代码如下:

MyApp.java:

 

[html]  view plain copy
  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:

 

 

[html]  view plain copy
  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:

 

 

[html]  view plain copy
  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:

 

 

[html]  view plain copy
  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对象进行声明:

 

 

[html]  view plain copy
  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.  

更多相关文章

  1. 命令数据库在Android中查看和管理sqlite数据库
  2. Android入门篇四:使用剪切板在Activity之间传递对象数据
  3. Study on Android【二】--ContentProvider数据模型概述
  4. Android(安卓)Content Provider
  5. Android(安卓)- This Handler class should be static or leaks
  6. Android之Handler
  7. (超详细)android中SqLite数据库的使用(一文包懂包会)
  8. mybatisplus的坑 insert标签insert into select无参数问题的解决
  9. python起点网月票榜字体反爬案例

随机推荐

  1. android 版本更新和下载安装 适配android
  2. android菜鸟学习笔记16----Android项目打
  3. Android系列之Wifi定位
  4. AsyncTask使用和源码解析
  5. android开发每日汇总【2011-11-10】
  6. android 反编译
  7. 系出名门Android(5) - 控件(View)之TextV
  8. 如何关闭android中的HandlerThread
  9. android 使用Intent传递数据之返回结果
  10. java/android 使用swig编译c/c++ 代码类