android中application 关于全局变量
来源:互联网 发布:大数据培训机构排名 编辑:程序博客网 时间:2024/06/06 12:34
android编程中,application这样的名词似乎变得那样的不常见,而让大家更为熟悉的是activity、intent、provider、broadcast和service。但其实android中的application也有着它自身的用处。
打开manifest文件,会看到有一个application配置标签,这就是有关application的使用了。那究竟application有什么用处呢?来看看SDK中是如何描述的:
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.
就是说application是用来保存全局变量的,并且是在package创建的时候就跟着存在了。所以当我们需要创建全局变量的时候,不需要再像 j2se那样需要创建public权限的static变量,而直接在application中去实现。只需要调用Context的 getApplicationContext或者Activity的getApplication方法来获得一个application对象,再做出相应的处理。
因小工程中涉及到的文件比较多,这里就贴代码撒。
application文件:
Java代码:
public class TestApplication extends Application {
private int curIndex;
public int getCurIndex() {
return curIndex;
}
public void setCurIndex(int curIndex) {
this.curIndex = curIndex;
}
@Override
public void onCreate() {
super.onCreate();
}
@Override
public void onTerminate() {
super.onTerminate();
}
}
application中有一个curIndex和setter getter方法。
第一个acitivty中对application进行的操作:
Java代码:
TestApplication application = (TestApplication) this.getApplication();
Log.i("data", ""+application.getCurIndex());
application.setCurIndex(5);
第二个Activity:
Java代码:
TestApplication application = (TestApplication)this.getApplication();
Log.i("data", ""+application.getCurIndex());
application.setCurIndex(6);
第三个Activity:
Java代码
final TestApplication application = (TestApplication) this.getApplication();
Log.i("data", ""+application.getCurIndex());
在运行过程中,每一次都kill掉对应的Activity,再进入下一个Activity。运行的结果如下:
- android中application 关于全局变量
- android中application 关于全局变量
- android中application 关于全局变量
- android中application 关于全局变量
- Android中application 全局变量
- Android中application 全局变量
- Android中Application设置全局变量
- 在android.app.Application中定义全局变量
- 在android.app.Application中定义全局变量
- 在android.app.Application中定义全局变量
- 在android.app.Application中定义全局变量
- 在android.app.Application中定义全局变量
- 在android.app.Application中定义全局变量
- 在android.app.Application中定义全局变量
- 在Android中使用Application保存全局变量
- 在android.app.Application中定义全局变量
- android中运用Application实现全局变量
- android中通过Application设置全局变量
- matlab normalized pixel Intensities
- Weblogic Server Console密码修改和遗忘解决方法
- 一天你的学习时间有多少?
- ORACLE SGA问题分析
- 第五章 循环结构
- android中application 关于全局变量
- linux grep命令参数及用法详解---管道命令grep
- Oracle数据库常用函数(二)
- php ini set 相关的设置项有四个变更域
- 集合
- Android数据缓冲区和数据流的学习总结(BufferedWriter、BufferedOutputStream和FileOutputStream)
- http://acm.nyist.net/JudgeOnline/status.php?pid=290
- 两款实用免费的文档免积分下载工具,支持百度文库、豆丁网等
- Python debug 模块之pdb--(一)