【记录】Android中的Application

移动开发 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/android-8/source目录中,所以Pkg.SourceUrl=sources):

### Android Tool: Source of this archive.
#Mon Apr 23 01:14:48 CST 2012
Layoutlib.Api=4
Layoutlib.Revision=0
Pkg.Desc=Android SDK Platform 2.2_r3
Archive.Arch=ANY
Platform.Version=2.2
Pkg.DescUrl=http\://developer.android.com/sdk/
Platform.MinToolsRev=8
Archive.Os=ANY
Pkg.SourceUrl=sources
Pkg.Revision=3
AndroidVersion.ApiLevel=8

【编辑推荐】

  1. 通过Titanium将Metro UI移植到iOS/Android上
  2. Titanium中Android模块开发:@Kroll注解详解
  3. tanium中Android模块开发:移植到1.8版本上
责任编辑:冰凝儿 来源: 博客园
相关推荐

2009-06-16 15:20:48

ApplicationJSP程序

2009-06-14 22:22:02

ibmdwWebSphere

2009-06-29 17:57:30

ApplicationJSP

2009-08-14 17:08:00

Android应用程序

2011-07-07 17:30:25

iPhone Xcode

2021-11-23 09:09:27

Applicationandroid系统开发

2021-11-19 17:26:11

AppApplication方法

2017-04-27 10:20:13

Android内存优化图片

2019-11-26 14:00:58

awkLinux命令

2009-06-14 16:59:16

ibmdwWebSphere

2009-06-14 17:04:02

ibmdwWebSphere

2009-09-22 12:08:45

ibmdwWebSphere

2010-04-28 18:25:51

Oracle数据库

2010-11-11 10:53:22

SQL Server遍

2021-09-02 09:24:14

DNSrrDNS安全工具

2011-06-01 14:20:06

Android Selector

2011-03-28 10:56:12

ibmdwRational

2018-07-03 14:00:44

Linux终端script命令

2011-08-11 11:51:39

MySQLselect

2011-04-06 16:25:47

SQL Server
点赞
收藏

51CTO技术栈公众号