Applicationç±»
ApplicationåActivity,Serviceä¸æ ·æ¯Androidæ¡æ¶çä¸ä¸ªç³»ç»ç»ä»¶ï¼å½Androidç¨åºå¯å¨æ¶ç³»ç»ä¼å建ä¸ä¸ªApplication对象ï¼ç¨æ¥åå¨ç³»ç»çä¸äºä¿¡æ¯ã
Androidç³»ç»èªå¨ä¼ä¸ºæ¯ä¸ªç¨åºè¿è¡æ¶å建ä¸ä¸ªApplicationç±»ç对象ä¸åªå建ä¸ä¸ªï¼æ以Applicationå¯ä»¥è¯´æ¯åä¾ï¼singletonï¼æ¨¡å¼çä¸ä¸ªç±»ã
é常æ们æ¯ä¸éè¦æå®ä¸ä¸ªApplicationçï¼ç³»ç»ä¼èªå¨å¸®æ们å建ï¼å¦æéè¦å建èªå·±çApplicationï¼é£ä¹å¾ç®åï¼å建ä¸ä¸ªç±»ç»§æ¿Application并å¨AndroidManifest.xmlæ件ä¸çapplicationæ ç¾ä¸è¿è¡æ³¨åï¼åªéè¦ç»applicationæ ç¾å¢å nameå±æ§ï¼å¹¶æ·»å èªå·±ç Applicationçååå³å¯ï¼ã
å¯å¨Applicationæ¶ï¼ç³»ç»ä¼å建ä¸ä¸ªPIDï¼å³è¿ç¨IDï¼ææçActivityé½ä¼å¨æ¤è¿ç¨ä¸è¿è¡ãé£ä¹æ们å¨Applicationå建çæ¶ååå§åå
¨å±åéï¼åä¸ä¸ªåºç¨çææActivityé½å¯ä»¥åå°è¿äºå
¨å±åéçå¼ï¼æ¢å¥è¯è¯´ï¼æ们å¨æä¸ä¸ªActivityä¸æ¹åäºè¿äºå
¨å±åéçå¼ï¼é£ä¹å¨åä¸ä¸ªåºç¨çå
¶ä»Activityä¸å¼å°±ä¼æ¹åã
Application对象ççå½å¨ææ¯æ´ä¸ªç¨åºä¸æé¿çï¼å®ççå½å¨æå°±çäºè¿ä¸ªç¨åºççå½å¨æãå 为å®æ¯å
¨å±çåä¾çï¼æ以å¨ä¸åçActivity,Serviceä¸è·å¾ç对象é½æ¯åä¸ä¸ªå¯¹è±¡ãæ以å¯ä»¥éè¿Applicationæ¥è¿è¡ä¸äºï¼å¦ï¼æ°æ®ä¼ éãæ°æ®å
±äº«åæ°æ®ç¼åçæä½ã
åºç¨åºæ¯ï¼
å¨Androidä¸ï¼å¯ä»¥éè¿ç»§æ¿Applicationç±»æ¥å®ç°åºç¨ç¨åºçº§çå
¨å±åéï¼è¿ç§å
¨å±åéæ¹æ³ç¸å¯¹éæç±»æ´æä¿éï¼ç´å°åºç¨çææActivityå
¨é¨è¢«destoryæä¹åæä¼è¢«éæ¾æã
å®ç°æ¥éª¤ï¼
1ã继æ¿Application
public class CustomApplication extends Application
{
private static final String VALUE = "Harvey";
private String value;
@Override
public void onCreate()
{
super.onCreate();
setValue(VALUE); // åå§åå
¨å±åé
}
public void setValue(String value)
{
this.value = value;
}
public String getValue()
{
return value;
}
}
注ï¼ç»§æ¿Applicationç±»ï¼ä¸»è¦éåéé¢çonCreateï¼ï¼æ¹æ³ï¼android.app.Applicationå
çonCreateï¼ï¼ææ¯çæ£çAndroidç¨åºçå
¥å£ç¹ï¼ï¼å°±æ¯å建çæ¶åï¼åå§ååéçå¼ãç¶åå¨æ´ä¸ªåºç¨ä¸çå个æ件ä¸å°±å¯ä»¥å¯¹è¯¥åéè¿è¡æä½äºã
2ãå¨ApplicationManifest.xmlæ件ä¸é
ç½®èªå®ä¹çApplication
<application
android:name="CustomApplication">
</application>
å®ä¾ä»£ç ï¼
CustomApplication.java
/**
* 继æ¿Application
*
* @author admin
*
*/
public class CustomApplication extends Application
{
private static final String VALUE = "Harvey";
private String value;
@Override
public void onCreate()
{
super.onCreate();
setValue(VALUE); // åå§åå
¨å±åé
}
public void setValue(String value)
{
this.value = value;
}
public String getValue()
{
return value;
}
}
FirstActivity.java
public class FirstActivity extends Activity
{
private CustomApplication app;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
app = (CustomApplication) getApplication(); // è·å¾CustomApplication对象
Log.i("FirstActivity", "åå§å¼=====" + app.getValue()); // è·åè¿ç¨ä¸çå
¨å±åéå¼ï¼çæ¯å¦æ¯åå§åå¼
app.setValue("Harvey Ren"); // éæ°è®¾ç½®å¼
Log.i("FirstActivity", "ä¿®æ¹å=====" + app.getValue()); // å次è·åè¿ç¨ä¸çå
¨å±åéå¼ï¼çæ¯å¦è¢«ä¿®æ¹
Intent intent = new Intent();
intent.setClass(this, SecondActivity.class);
startActivity(intent);
}
}
注ï¼åªéè¦è°ç¨Contextç getApplicationContextæè
ActivityçgetApplicationæ¹æ³æ¥è·å¾ä¸ä¸ªApplication对象ï¼ç¶ååå¾å°ç¸åºçæååéå³å¯ãå®æ¯ä»£è¡¨æ们çåºç¨ç¨åºçç±»ï¼ä½¿ç¨å®å¯ä»¥è·å¾å½ååºç¨ç主é¢åèµæºæ件ä¸çå
容çï¼è¿ä¸ªç±»æ´çµæ´»çä¸ä¸ªç¹æ§å°±æ¯å¯ä»¥è¢«æ们继æ¿ï¼æ¥æ·»å æ们èªå·±çå
¨å±å±æ§ã
SecondActivity.java
public class SecondActivity extends Activity
{
private CustomApplication app;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
app = (CustomApplication) getApplication(); // è·ååºç¨ç¨åº
Log.i("SecondActivity", "å½åå¼=====" + app.getValue()); // è·åå
¨å±å¼
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="
http://schemas.android.com/apk/res/android"
package="com.android.test"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk
android:minSdkVersion="8" />
<application
android:icon="@drawable/icon"
android:label="@string/app_name"
android:name="CustomApplication">
<!-- å°æ们以åä¸ç´ç¨çé»è®¤Application设置æèªå®ä¹çCustomApplication -->
<activity
android:name=".FirstActivity"
android:label="@string/app_name">
<intent-filter>
<action
android:name="android.intent.action.MAIN" />
<category
android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".SecondActivity"
android:label="@string/app_name">
</activity>
</application>
</manifest>