最近在项目中,遇到了application这个类,开始不知道有什么用,通过学习后才知道它的用途也蛮大的,举个例子,若是想在整个应用中使用全局变量,在java中通常是使用静态变量,public类型;而在android中若是使用这样的全局变量就不符合Android的框架架构,可是可使用一种更优雅的方式就是使用Application context。html
咱们先看看一下这段说明:java
Base class for those who need to maintain global application state. You android
can provide your own implementation by specifying its name in your架构
AndroidManifest.xml's <application> tag, which will cause that classapp
to be instantiated for you when the process for your application/package is框架
created. ide
意思是:application类是一个基类,这个基类的做用是为了获取整个应用程序的状态。你能够本身继承或实现这个类,当你要使用本身拓展的application类的时候,只要在manifest.xml中的<application>标签中name应用本身定义的类就好了,这样作的结果是:当你的应用程序或者包所在的进程建立的时候,这个类就会被实例化。学习
怎么使用它呢?首先须要重写Application,主要重写里面的onCreate方法,就是建立的时候,初始化变量的值。而后在整个应用中的各个文件中就能够对该变量进行操做了。 启动Application时,系统会建立一个PID,即进程ID,全部的Activity就会在此进程上运行。那么咱们在Application建立的时候初始化全局变量,同一个应用的全部Activity均可以取到这些全局变量的值,换句话说,咱们在某一个Activity中改变了这些全局变量的值,那么在同一个应用的其余Activity中值就会改变。下面举个例子详细介绍一下应用步骤:this
代码以下:spa
- yy.android.yapp;
- import android.app.Application;
-
- public class YApp extends Application{
-
- private String ylabel ;
- public String getLabel(){
- return ylabel;
- }
- public void setLabel(String s){
- this.ylabel = s;
- }
-
- @Override
- public void onCreate() {
-
- super.onCreate();
- setLabel("YUZHIBOYI_CSND!");
- }
- }
下面是mainActivity.java
- package yy.android.yapp;
-
- import android.app.Activity;
- import android.content.Intent;
- import android.os.Bundle;
- import android.util.Log;
-
- public class mainActivity extends Activity {
-
- private YApp yApp;
-
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- yApp = (YApp) getApplication();
- Log.i("YAnGl", "InitLabel:"+yApp.getLabel());
-
- yApp.setLabel("YUZHIBOYI!");
- Log.i("YAnG", "ChangeLabel:"+yApp.getLabel());
-
- Intent intent = new Intent();
- intent.setClass(this, otherActivity.class);
- startActivity(intent);
- }
- }
另外一个otherActivity.java:
- package yy.android.yapp;
-
- import android.app.Activity;
- import android.os.Bundle;
- import android.util.Log;
-
- public class otherActivity extends Activity{
-
- private YApp yApp;
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
-
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
-
- yApp = (YApp) getApplication();
- Log.i("YAnG", "OhterActivity receive the Label:"+yApp.getLabel());
-
- }
- }
修改配置文件ApplicationManifest.xml,将要运行的应用程序YApp加进去:
- <?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">
-
- <application android:name="YApp"
- android:icon="@drawable/icon"
- android:label="@string/app_name"
- >
- <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>
- <activity android:name=".otherActivity">
- </activity>
- </application>
-
- </manifest>
运行的结果: 03-04 16:53:17.295: INFO/guoll(650): InitLabel:YUZHIBOYI_CSND! 03-04 16:53:17.295: INFO/guoll(650): ChangeLabel:YUZHIBOYI 03-04 16:53:17.426: INFO/guoll(650): OhterActivity receive the Label:YUZHIBOYI
好了,用法就这样!