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

    博客分类:
  • Android开发
Android OS XML 框架 如果想在整个应用中使用全局变量,在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!

更多相关文章

  1. android shape的使用
  2. Android(安卓)Dialog对话框的七种形式的使用
  3. Android应用程序键盘(Keyboard)消息处理机制分析(1)
  4. 《Android移动应用基础教程》(Android(安卓)Studio)(第二版)黑马教程
  5. Android(安卓)虚拟多开系列二——技术原理
  6. Android(安卓)Studio上非常棒的插件
  7. Android"重力加速度传感器"从驱动到应用层全程分析
  8. 【教程连载】ArcGIS Runtime for Android开发教程V2.0(1)基本概念
  9. [置顶] Mc小冰总结的Android开发工程师面试题以及答案,android程

随机推荐

  1. View 控件EditText属性
  2. Android内存管理基本介绍
  3. 《Android开发从零开始》——13.Table La
  4. 常用知识篇 一 Selector state状态对应说
  5. Android中定义样式(1)
  6. [Android] TextView只显示一行,多余显示
  7. ANDROID样式的开发:SHAPE篇
  8. Android中gravity与layout_gravity的区别
  9. EventBus 《二》 Android(安卓)EventBus
  10. Android(安卓)安全加密:对称加密详解