android中application 關於全域變數

來源:互聯網
上載者:User

android編程中,application這樣的名詞似乎變得那樣的不常見,而讓大家更為熟悉的是activity、intent、provider、broadcast和service。但其實android中的application也有著它自身的用處。

  開啟manifest檔案,會看到有一個application配置標籤,這就是有關application的使用了。那究竟application有什麼用處呢?來看看SDK中是如何描述的:

  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是用來儲存全域變數的,並且是在package建立的時候就跟著存在了。所以當我們需要建立全域變數的時候,不需要再像 j2se那樣需要建立public許可權的static變數,而直接在application中去實現。只需要調用Context的 getApplicationContext或者Activity的getApplication方法來獲得一個application對象,再做出相應的處理。

  因小工程中涉及到的檔案比較多,這裡就貼代碼撒。

  application檔案:

  Java代碼

  1. public class TestApplication extends Application {

  2.

  3. private int curIndex;

  4.

  5. public int getCurIndex() {

  6. return curIndex;

  7. }

  8.

  9. public void setCurIndex(int curIndex) {

  10. this.curIndex = curIndex;

  11. }

  12.

  13. @Override

  14. public void onCreate() {

  15. super.onCreate();

  16. }

  17.

  18. @Override

  19. public void onTerminate() {

  20. super.onTerminate();

  21. }

  22.

  23. }

  public class TestApplication extends Application {

  private int curIndex;

  public int getCurIndex() {

  return curIndex;

  }

  public void setCurIndex(int curIndex) {

  this.curIndex = curIndex;

 

  }

  @Override

  public void onCreate() {

  super.onCreate();

  }

  @Override

  public void onTerminate() {

  super.onTerminate();

  }

  }

  application中有一個curIndex和setter getter方法。

  第一個acitivty中對application進行的操作:

  Java代碼

  1. TestApplication application = (TestApplication) this.getApplication();

  2. Log.i("data", ""+application.getCurIndex());

  3. application.setCurIndex(5);

  TestApplication application = (TestApplication) this.getApplication();

  Log.i("data", ""+application.getCurIndex());

  application.setCurIndex(5);

  第二個Activity:

  Java代碼

  1. TestApplication application = (TestApplication)this.getApplication();

  2. Log.i("data", ""+application.getCurIndex());

  3. application.setCurIndex(6);

  TestApplication application = (TestApplication)this.getApplication();

  Log.i("data", ""+application.getCurIndex());

  application.setCurIndex(6);

  第三個Activity:

  Java代碼

  1. final TestApplication application = (TestApplication) this.getApplication();

  2. Log.i("data", ""+application.getCurIndex());

  final TestApplication application = (TestApplication) this.getApplication();

  Log.i("data", ""+application.getCurIndex());

 

PS:   <application android:icon="@drawable/icon" android:label="@string/app_name" android:name=".TestApplication">

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.