Android Application 创建全局变量


以前都是建立一个ConstData的类来保存全局用的变量,但是有时候确实是有点小问题。

所以研究了一下使用Application来建立全局变量,下面就是代码,主要分为四个文件:

(1)是MyApplication类,保存全局变量以及变量的查询和修改

(2)TestAndroid 类 也是主类

(3)otherActivity 另外一个类调用全局变量试试是不是被主类改变了

(4)manifest.xml文件 

MyApplication

  1. package an.test.android;  
  2.   
  3. import android.app.Application;   
  4.   
  5. public class MyApp extends Application{   
  6.   
  7.     private String mylabel ;       
  8.     public String getLabel(){   
  9.         return mylabel;   
  10.     }      
  11.     public void setLabel(String s){   
  12.         this.mylabel = s;   
  13.     }   
  14.   
  15.     @Override   
  16.     public void onCreate() {   
  17.         // TODO Auto-generated method stub    
  18.         super.onCreate();   
  19.         setLabel("Welcome!"); //初始化全局变量           
  20.     }      
  21. }   
TestAndroid
  1. package an.test.android;  
  2.   
  3. import android.app.Activity;  
  4. import android.content.Intent;  
  5. import android.os.Bundle;  
  6. import android.util.Log;  
  7.   
  8. public class TestAndroid extends Activity {  
  9.     /** Called when the activity is first created. */  
  10.   
  11.       
  12.     private MyApp myApp;   
  13.       
  14.       
  15.     public void onCreate(Bundle savedInstanceState) {  
  16.         super.onCreate(savedInstanceState);  
  17.         setContentView(R.layout.main);  
  18.           
  19.         myApp = (MyApp) getApplication(); //获得自定义的应用程序MyApp    
  20.         Log.i("guoll""InitLabel:"+myApp.getLabel());   //将我们放到进程中的全局变量拿出来,看是不是我们曾经设置的值    
  21.   
  22.         myApp.setLabel("Changing!");  //修改一下    
  23.         Log.i("guoll""ChangeLabel:"+myApp.getLabel()); //看下,这个值改变了没有    
  24.   
  25.         Intent intent = new Intent();  //再看一下在另一个Activity中是取到初始化的值,还是取到修改后的值    
  26.         intent.setClass(this, otherActivity.class);   
  27.         startActivity(intent);   
  28.           
  29.           
  30.           
  31.     }  
  32. }  
otherActivity
  1. package an.test.android;  
  2.   
  3. import android.app.Activity;   
  4. import android.os.Bundle;   
  5. import android.util.Log;   
  6.   
  7. public class otherActivity extends Activity{   
  8.       
  9.     private MyApp myApp;   
  10.       
  11.     @Override   
  12.     protected void onCreate(Bundle savedInstanceState) {   
  13.   
  14.             super.onCreate(savedInstanceState);   
  15.             setContentView(R.layout.main);   
  16.               
  17.             myApp = (MyApp) getApplication();  //获得自定义的应用程序MyApp    
  18.             Log.i("guoll""OhterActivity receive the Label:"+myApp.getLabel()); //查看变量值是否修改了    
  19.   
  20.     }          
  21. }   
manifest.xml
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.       package="an.test.android"  
  4.       android:versionCode="1"  
  5.       android:versionName="1.0">  
  6.   
  7.       
  8.   
  9.     <application android:icon="@drawable/icon" android:label="@string/app_name" android:name="MyApp"  >  
  10.       
  11.         <activity android:name=".TestAndroid"  
  12.                   android:label="@string/app_name">  
  13.             <intent-filter>  
  14.                 <action android:name="android.intent.action.MAIN" />  
  15.                 <category android:name="android.intent.category.LAUNCHER" />  
  16.             </intent-filter>  
  17.         </activity>  
  18.           
  19.         <activity android:name=".otherActivity">   
  20.         </activity>   
  21.   
  22.     </application>  
  23. </manifest>  
这里尤其要注意的一点就是:MyApp这个类要在manifest中进行注册,其实就是加上了一行代码

android:name="MyApp"

但是这个是必须的!

更多Android相关信息见Android 专题页面 http://www.bkjia.com/topicnews.aspx?tid=11

相关内容