Android使用Application總結

來源:互聯網
上載者:User

標籤:

Application 配置全域Context

第一步、寫一個全域的單例模式的MyApplication繼承自Application 覆蓋onCreate ,在這個方法裡面執行個體化Application

第二步、配置全域的Context

<application android:name="com.appstore.service.MyApplication" ></application>

第三步、使用,使用的時候用的時候根據類的名稱訪問Context

Android程式的進入點

Android使用Google Dalvik VM,相對於傳統Java VM而言有著很大的不同,在Sun的Java體系中進入點和標準c語言一樣是main(),而每個Android程式都包含著一個Application執行個體,一個Application執行個體中有多個Activity、Service、ContentProvider或Broadcast Receiver。因為大部分的應用都包含Activity所以,說很多網友認為是Activity的onCreate,但是你沒有發現你的工程中有多個Activity嗎? 你可能沒有見過沒有Activity的Android應用吧。

其實在android.app.Application這個包的onCreate才是真正的Android進入點,只不過大多數開發人員無需重寫該類,他的繼承關係如:

java.lang.Object
? android.content.Context
? android.content.ContextWrapper
? android.app.Application

android.app.Application類包含了4個公開的方法

void  onConfigurationChanged(Configuration newConfig)
void  onCreate()  //這裡才是真正的進入點。
void  onLowMemory()
void  onTerminate()

所以希望大家,記住真正的Android進入點是application的main,你可以看下androidmanifest.xml的內含項目關聯性就清楚了,並不是每個應用都必須有Activity的。

android中application 關於全域變數

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代碼:

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代碼:

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

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

application.setCurIndex(5);

第二個Activity:

Java代碼:

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

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

application.setCurIndex(6);

第三個Activity:

Java代碼

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

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

在運行過程中,每一次都kill掉對應的Activity,再進入下一個Activity。

Android使用Application總結

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.