Application概念理解

官方释义:

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.

There is normally no need to subclass Application. In most situation, static singletons can provide the same functionality in a more modular way. If your singleton needs a global context (for example to register broadcast receivers), the function to retrieve it can be given a Context which internally uses Context.getApplicationContext() when first constructing the singleton.

个人翻译:

Application类是为了那些需要保存全局变量设计的基本类。你可以在AndroidManifest.xml的<application>标签中指定名称进行自己的实现,这样的结果是:当你的application或者包被建立的时候将引起那个类被建立。

子类Application通常是没有必要的。大多数情况下,静态的单例可以通过更加模块化的方式提供相同的功能,如果你的单例需要一个全局的Context(例如注册一个广播接收器),当第一次构建单例时,程序内部使用Context.getApplication()方法可以返回一个Context(上下文)。

个人理解:

Application是用来保存全局变量的,并且是在package创建的时候就跟着存在了。所以当我们需要创建全局变量的时候,直接在Application中去实现。只需要调用Context的getApplicationContext或者Activity的getApplication方法来就可以获得一个Application对象。

接下来通过例子来看一下具体用法:

GlobalParameterApplication.java

package com.dt5000.ischool.util;import android.app.Application;import com.dt5000.ischool.bean.User;/** * @author: duanyr * @创建时间: 2012-11-14 上午10:50:45 *  * 类说明:全局参数类 */public class GlobalParameterApplication extends Application {private User user;public User getUser() {return user;}public void setUser(User user) {this.user = user;}}
package com.dt5000.ischool.activity;import android.app.Activity;import com.dt5000.ischool.bean.User;import com.dt5000.ischool.util.GlobalParameterApplication;public class UserLoginActivity extends Activity {@Overrideprotected void onStart() {// TODO Auto-generated method stubUser user = new User();user.setUserName("example");GlobalParameterApplication gpa = (GlobalParameterApplication) getApplicationContext();gpa.setUser(user);// 将User信息放入到Applicationgpa.getUser();//取出User信息super.onStart();}}

AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.dt5000.ischool.activity"    android:versionCode="1"    android:versionName="1.0" >    <uses-sdk        android:minSdkVersion="8"        android:maxSdkVersion="15" android:targetSdkVersion="8" />    <application        android:name="com.dt5000.ischool.util.GlobalParameterApplication"        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@android:style/Theme.NoTitleBar" >        <activity            android:name=".WelcomeActivity"            android:label="@string/title_activity_welcome" >            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>                    </activity>    </application></manifest>


注意:不用新建<application />,在原有基础上添加内容:android:name=".your_App_Name"



更多相关文章

  1. android 使用Intent传递数据之全局变量传递
  2. Android 替换全局字体
  3. Android用户界面UI组件--AdapterView及其子类(四) GridView
  4. c++如何设置全局变量
  5. C语言中在main函数中定义的变量是全局变量么
  6. golang-cache是全局吗
  7. 关于Golang 全局sql数据库连接
  8. Spring Boot 工程集成全局唯一ID生成器 Vesta
  9. Spring Boot工程集成全局唯一ID生成器 UidGenerator

随机推荐

  1. 百度云推送、极光推送等云推送服务搜罗点
  2. android studio引入最新版银联支付功能
  3. android事件分发(二)
  4. Android(安卓)添加新的键值,自定义按键
  5. Windows phone开发初体验之(四)-处理Wind
  6. Android的国际化语言适配(系统语言适配+A
  7. android 线程消息处理【之消息处理中再处
  8. Android(安卓)调试桥|adb命令卸载系统软
  9. 【android studio】安卓中修改每个每个活
  10. android中单位的简单介绍