最近在项目中,遇到了 Application 这个类,开始不知道有什么用,经过学习后才知道它的用途也蛮大的,举个例子,如果想在整个应用中使用全局变量,在 Java 中一般是使用静态变量,public类型;而在 Android 中如果使用这样的全局变量就不符合 Android 的框架架构,但是可以使用一种更优雅的方式,就是使用 Application Context。

我们先来看看一下这段说明:

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 类是一个基类,这个基类的作用是为了获取整个应用程序的状态。你可以自己继承或实现这个类,当你要使用自己拓展的 Application 类的时候,只要在AndroidManifest.xml 中的 <application> 标签中 name 应用自己定义的类就行了,这样做的结果是:当你的应用程序或者包所在的进程创建的时候,这个类就会被实例化。


怎么使用它呢?首先需要重写 Application,主要重写里面的 onCreate() 方法,就是创建的时候,初始化变量的值。然后在整个应用中的各个文件中就可以对该变量进行操作了。启动 Application 时,系统会创建一个 PID,即进程 ID,所有的 Activity 就会在此进程上运行。那么我们在 Application 创建的时候初始化全局变量,同一个应用的所有 Activity 都可以取到这些全局变量的值,换句话说,我们在某一个 Activity 中改变了这些全局变量的值,那么在同一个应用的其他Activity 中值就会改变。


下面举个例子详细介绍一下应用步骤:

自定义的 QApp:

import android.app.Application;    public class QApp extends Application{        private String label;            public String getLabel(){          return ylabel;      }        public void setLabel(String s){          this.label = s;      }        @Override      public void onCreate() {          // TODO Auto-generated method stub          super.onCreate();          setLabel("Hello, Qing!");   //初始化全局变量      }    }  

MainActivity:
import android.app.Activity;    import android.content.Intent;    import android.os.Bundle;    import android.util.Log;    public class MainActivity extends Activity {        private QApp qApp;        @Override      public void onCreate(Bundle savedInstanceState) {          super.onCreate(savedInstanceState);          setContentView(R.layout.main);            qApp = (QApp) getApplication();  // 获得自定义的应用程序 QApp          Log.i("QLog", "InitLabel:" + qApp.getLabel());  // 将我们放到进程中的全局变量拿出来,看是不是我们曾经设置的值                    qApp.setLabel("Set Label");  //修改一下          Log.i("QLog", "ChangeLabel:" + qApp.getLabel());  // 看下,这个值改变了没有            Intent intent = new Intent();  // 再看一下在另一个 Activity中 是取到初始化的值,还是取到修改后的值          intent.setClass(this, otherActivity.class);          startActivity(intent);      }    }  

OtherActivity:

import android.app.Activity;    import android.os.Bundle;    import android.util.Log;    public class OtherActivity extends Activity{        private QApp qApp;        @Override      protected void onCreate(Bundle savedInstanceState) {          super.onCreate(savedInstanceState);          setContentView(R.layout.main);            qApp = (QApp) getApplication();  // 获得自定义的应用程序 QApp          Log.i("QLog", "OhterActivity receive the Label:" + qApp.getLabel());  // 查看变量值是否修改了      }    }  

修改配置文件 ApplicationManifest.xml,将要运行的应用程序 QApp 加进去:

<?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 设置成自己做的 QApp-->      <application android:name=".QApp"          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>  


更多相关文章

  1. android四层框架
  2. Android中实现全屏、无标题栏的两种办法(另附Android系统自带样式
  3. 关于Android的自动化测试,你需要了解的5个测试框架
  4. Android贝壳单词客户端应用源码
  5. Android中的Intent
  6. Android(安卓)Service相关知识
  7. Android的系统架构
  8. 谷歌Android智能手机成应用开发者的宠儿
  9. Android(安卓)Framework(一) 系统架构及源码

随机推荐

  1. Android(安卓)AsyncTask
  2. Andrioid SystemProperties和Settings.Sy
  3. Android持久化之SharedPreferences
  4. android 开发环境搭建
  5. Android(安卓)内核源代码交叉编译
  6. android引入JAR包,打包成JAR包,打包成Libra
  7. android解析xml文档的各种方法
  8. android屏幕旋转时,Activity不重新调用onC
  9. Android进阶-Android动画机制与使用技巧
  10. 国内更新Android(安卓)SDK 使用Android(