现在的位置: 首页 > 综合 > 正文

Android用Application设置全局变量以及使用

2013年12月09日 ⁄ 综合 ⁄ 共 3013字 ⁄ 字号 评论关闭

如果想在整个应用中使用全局变量,在java中一般是使用静态变量,public类型;而在android中如果使用这样的全局变量就不符合Android的框架架构,但是可以使用一种更优雅的方式就是使用Application context。

  首先需要重写Application,主要重写里面的onCreate方法,就是创建的时候,初始化变量的值。然后在整个应用中的各个文件中就可以对该变量进行操作了。

  启动Application时,系统会创建一个PID,即进程ID,所有的Activity就会在此进程上运行。那么我们在Application创建的时候初始化全局变量,同一个应用的所有Activity都可以取到这些全局变量的值,换句话说,我们在某一个Activity中改变了这些全局变量的值,那么在同一个应用的其他Activity中值就会改变。下面举个例子详细介绍一下应用步骤。

下面是MyApp.java

package com.android.test;
import android.app.Application;

public class MyApp extends Application{

    private String mylabel ;    
    public String getLabel(){
        return mylabel;
    }   
    public void setLabel(String s){
        this.mylabel = s;
    }

    @Override
    public void onCreate() {
        // TODO Auto-generated method stub
        super.onCreate();
        setLabel("Welcome!"); //初始化全局变量       
    }   
}

下面是mainActivity.java
package com.ginwave.test;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

public class mainActivity extends Activity {
   
    private MyApp myApp;
   
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        myApp = (MyApp) getApplication(); //获得自定义的应用程序MyApp
        Log.i("guoll", "InitLabel:"+myApp.getLabel());   //将我们放到进程中的全局变量拿出来,看是不是我们曾经设置的值

        myApp.setLabel("Changing!");  //修改一下
        Log.i("guoll", "ChangeLabel:"+myApp.getLabel()); //看下,这个值改变了没有

        Intent intent = new Intent();  //再看一下在另一个Activity中是取到初始化的值,还是取到修改后的值
        intent.setClass(this, otherActivity.class);
        startActivity(intent);
    }
}

另一个otherActivity.java:

package com.android.test;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

public class otherActivity extends Activity{
   
    private MyApp myApp;
   
    @Override
    protected void onCreate(Bundle savedInstanceState) {

            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
           
            myApp = (MyApp) getApplication();  //获得自定义的应用程序MyApp
            Log.i("guoll", "OhterActivity receive the Label:"+myApp.getLabel()); //查看变量值是否修改了

    }       
}

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

<?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设置成自己做的MyApp-->
    <application android:name="MyApp"
        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:Welcome!
03-04 16:53:17.295: INFO/guoll(650): ChangeLabel:Changing!
03-04 16:53:17.426: INFO/guoll(650): OhterActivity receive the Label:Changing!

抱歉!评论已关闭.