当前位置:操作系统 > 安卓/Android >>

Android Application (全局变量)学习使用

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
[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; 
    } 
 

[html] l.activity; 
 
import android.app.Activity; 
 
import com.dt5000.ischool.bean.User; 
import com.dt5000.ischool.util.GlobalParameterApplication; 
 
public class UserLoginActivity extends Activity { 
    @Override 
    protected void onStart() { 
        // TODO Auto-generated method stub 
         
        User user = new User(); 
        user.setUserName("example"); 
        GlobalParameterApplication gpa = (GlobalParameterApplication) getApplicationContext(); 
        gpa.setUser(user);// 将User信息放入到Application 
        gpa.getUser();//取出User信息         
        super.onStart(); 
    } 

AndroidManifest.xml
[html]
<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"


 

补充:移动开发 , Android ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,