您的位置:首页 > 其它

安卓开发中全局变量的创建

2016-03-03 09:44 323 查看
安卓APP开发过程中, 有的时候我们在应用程序的任何一个地方都需要访问一个全局变量,也就是在任何一个Activity中都可以访问的变量。它不会因为Activity的生命周期结束而消失。要实现应用程序级的变量,我们可以通过Application这个类来实现。

java:

package com.example.dazuoye;

import android.app.Application;

public class quanjubianliang extends Application {
private int curIndex;

public int getCurIndex() {
return curIndex;
}

public void setCurIndex(int curIndex) {
this.curIndex = curIndex;
}

}

然后在Androidmainfest.xml中

在<Application>中插入android:name=“你的java名字”

而在Activity中灵活使用即可

[java]
((quanjubianliang) getApplicationContext()).setCurIndex(要传入的数据);

下次调用

int ad=((quanjubianliang) getApplicationContext()).getCurIndex();

Application是与应用同时存在的,也就是应用在它就在。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: