Android中用Application類實現全局變量


最近在項目中,遇到了application這個類,開始不知道有什么用,經過學習后才知道它的用途也蠻大的,舉個例子,如果想在整個應用中使用全局變量,在java中一般是使用靜態變量,public類型;而在android中如果使用這樣的全局變量就不符合Android的框架架構,但是可以使用一種更優雅的方式就是使用Application context。

        我們先看看一下這段說明:

               Base class for those who need to maintain global application state. You

               can  provide your own implementation by specifying its name in your

               AndroidManifest.xml's <application> tag, which will cause that class

               to be instantiated for you when the process for your application/package is

                created.

        意思是:application類是一個基類,這個基類的作用是為了獲取整個應用程序的狀態。你可以自己繼承或實現這個類,當你要使用自己拓展的application類的時候,只要在manifest.xml中的<application>標簽中name應用自己定義的類就行了,這樣做的結果是:當你的應用程序或者包所在的進程創建的時候,這個類就會被實例化。

       怎么使用它呢?首先需要重寫Application,主要重寫里面的onCreate方法,就是創建的時候,初始化變量的值。然后在整個應用中的各個文件中就可以對該變量進行操作了。        啟動Application時,系統會創建一個PID,即進程ID,所有的Activity就會在此進程上運行。那么我們在Application創建的時候初始化全局變量,同一個應用的所有Activity都可以取到這些全局變量的值,換句話說,我們在某一個Activity中改變了這些全局變量的值,那么在同一個應用的其他Activity中值就會改變。下面舉個例子詳細介紹一下應用步驟:

代碼如下:

  1. yy.android.yapp;  
  2. import android.app.Application;  
  3.   
  4. public class YApp extends Application{  
  5.   
  6.     private String ylabel ;      
  7.     public String getLabel(){  
  8.         return ylabel;  
  9.     }     
  10.     public void setLabel(String s){  
  11.         this.ylabel = s;  
  12.     }  
  13.   
  14.     @Override  
  15.     public void onCreate() {  
  16.         // TODO Auto-generated method stub  
  17.         super.onCreate();  
  18.         setLabel("YUZHIBOYI_CSND!"); //初始化全局變量         
  19.     }     
  20. }  

下面是mainActivity.java

  1. package yy.android.yapp;  
  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 mainActivity extends Activity {  
  9.      
  10.     private YApp yApp;  
  11.      
  12.     @Override  
  13.     public void onCreate(Bundle savedInstanceState) {  
  14.         super.onCreate(savedInstanceState);  
  15.         setContentView(R.layout.main);  
  16.         yApp = (YApp) getApplication(); //獲得自定義的應用程序YApp  
  17.         Log.i("YAnGl", "InitLabel:"+yApp.getLabel());   //將我們放到進程中的全局變量拿出來,看是不是我們曾經設置的值  
  18.   
  19.         yApp.setLabel("YUZHIBOYI!");  //修改一下  
  20.         Log.i("YAnG", "ChangeLabel:"+yApp.getLabel()); //看下,這個值改變了沒有  
  21.   
  22.         Intent intent = new Intent();  //再看一下在另一個Activity中是取到初始化的值,還是取到修改后的值  
  23.         intent.setClass(this, otherActivity.class);  
  24.         startActivity(intent);  
  25.     }  
  26. }  

另一個otherActivity.java:

  1. package yy.android.yapp;  
  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 YApp yApp;  
  10.      
  11.     @Override  
  12.     protected void onCreate(Bundle savedInstanceState) {  
  13.   
  14.             super.onCreate(savedInstanceState);  
  15.             setContentView(R.layout.main);  
  16.              
  17.             yApp = (YApp) getApplication();  //獲得自定義的應用程序MyApp  
  18.             Log.i("YAnG", "OhterActivity receive the Label:"+yApp.getLabel()); //查看變量值是否修改了  
  19.   
  20.     }         
  21. }  

修改配置文件ApplicationManifest.xml,將要運行的應用程序YApp加進去:

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.       package="com.android.test"  
  4.       android:versionCode="1"  
  5.       android:versionName="1.0">  
  6.     <!-- 在這里,將默認的Application設置成自己做的YApp-->  
  7.     <application android:name="YApp"  
  8.         android:icon="@drawable/icon"  
  9.         android:label="@string/app_name"  
  10.         >  
  11.         <activity android:name=".mainActivity"  
  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.         <activity android:name=".otherActivity">  
  19.         </activity>  
  20.     </application>  
  21.   
  22. </manifest>  

運行的結果: 03-04 16:53:17.295: INFO/guoll(650): InitLabel:YUZHIBOYI_CSND! 03-04 16:53:17.295: INFO/guoll(650): ChangeLabel:YUZHIBOYI 03-04 16:53:17.426: INFO/guoll(650): OhterActivity receive the Label:YUZHIBOYI

好了,用法就這樣!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM