Android中的Application
先定义一个Application
package com.xesam;
import android.app.Application;
import android.content.res.Configuration;
import android.util.Log;
public class ApplicationActivity extends Application {
static String name = "小西山子";
@Override
public void onCreate() {
Log.v("ApplicationActivity","onCreate");
super.onCreate();
}
@Override
public void onTerminate() {
Log.v("ApplicationActivity","onTerminate");
super.onTerminate();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
Log.v("ApplicationActivity","onConfigurationChanged");
super.onConfigurationChanged(newConfig);
}
@Override
public void onLowMemory() {
Log.v("ApplicationActivity","onLowMemory");
super.onLowMemory();
}
}
主要Activity:
package com.xesam;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
Log.v("MainActivity","onCreate");
super.onCreate(savedInstanceState);
ApplicationActivity app = (ApplicationActivity)this.getApplication();
LinearLayout main = new LinearLayout(this);
main.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
main.setOrientation(LinearLayout.VERTICAL);
TextView tv = new TextView(this);
tv.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
tv.setText(app.name);
tv.setTextColor(Color.BLUE);
main.addView(tv);
setContentView(main);
}
@Override
public void onDestroy(){
Log.v("MainActivity","onDestroy");
super.onDestroy();
//System.exit(0);
}
}
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.xesam"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:name="ApplicationActivity">
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
这里我们自定义了一个Application,所以必须指定AndroidManifest.xml中application的android:name属性(默认是android.app.Application)。
输出结果:
"ApplicationActivity","onCreate"
"MainActivity","onCreate"
"MainActivity","小西山子"
"ApplicationActivity","onConfigurationChanged"
退出之后打印结果:
"MainActivity","onDestroy"
从模拟器再次开启应用(没有再次编译)之后,打印结果
"MainActivity","onCreate"
"MainActivity","小西山子"
所以后退的时候并没有关闭application,要强制关闭application,可以在MainActivity的onDestroy中调用System.exit(0)
@Override
public void onDestroy(){
Log.v("MainActivity","onDestroy");
super.onDestroy();
System.exit(0);
}
这样就完全退出程序了。
回到application的问题上来,显然,application的onCreate在activity的onCreate之前执行了,所以整个应用的最初入口是这个方法。
onCreate作用说明:
/**
* Called when the application is starting, before any other application
* objects have been created. Implementations should be as quick as
* possible (for example using lazy initialization of state) since the time
* spent in this function directly impacts the performance of starting the
* first activity, service, or receiver in a process.
* If you override this method, be sure to call super.onCreate().
*/
可以认为Activity,service以及其他的组件共同构成了一个application的环境,因此application中可以共享或者初始化一些全局变量,比如地图(百度地图demo就是这么做的)。
注:eclipse查看源码的方法:
下载android版本对应的源码【2.2版本地址:http://rgruet.free.fr/public/android-2.2-froyo-src.zip】
解压缩到 /usr/local/android-sdk-linux/platforms/android-8下,然后修改同目录下source.properties文件中的Pkg.SourceUrl为刚才解压的源码目录就行了
一个示例(我下载的源码放在/usr/local/android-sdk-linux/platforms/an
补充:移动开发 , Android ,