在Android中如何使用全局变量--Application context (转)

 我来答
QQMSD8
推荐于2018-05-15 · 知道合伙人软件行家
QQMSD8
知道合伙人软件行家
采纳数:6788 获赞数:13238
没有做不到,只有想不到,帮助别人的同时也是对自己的提升

向TA提问 私信TA
展开全部
可以将变量存放在Application中,Context,中文直译为“上下文”,SDK中对其说明如下:
Interface to global information about an application environment. This is an abstract class whose implementation
is provided by the Android system. It allows access to application-specific resources and classes, as well as up-calls
for application-level operations such as launching activities, broadcasting and receiving intents, etc。
从上可知一下三点即:

1、它描述的是一个应用程序环境的信息,即上下文。
2、该类是一个抽象(abstract class)类,Android提供了该抽象类的具体实现类(后面我们会讲到是ContextIml类)。
3、通过它我们可以获取应用程序的资源和类,也包括一些应用级别操作,例如:启动一个Activity,发送广播,接受Intent信息等。

以下为使用Application存储全局变量的示例代码:

1.继承Application,并在Application里声明全局变量。
public class MyApplication extends Application {
private User user;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
}

2.在AndroidManifest.xml的application节点中声明这个Application。
<application android:name="com.xxx.xxx.MyApplication">

3.在Activity中获取Application对象,并存取全局变量。
User user = new User();
user.setUserName("example");
MyrApplication app= (MyApplication ) getApplicationContext();
app.setUser(user); //将变量存到application
User tmp = app.getUser();//从application中读取全局变量。
雪的桃脏B兇
推荐于2016-07-07 · 超过59用户采纳过TA的回答
知道答主
回答量:177
采纳率:100%
帮助的人:129万
展开全部
做Java的人肯定都用过全局变量了,使用方法无非是定义一个静态变量,public类型,这样在其他类中就可以直接调用了,android中也可以这样使用。 但是,android中这样使用全局变量是不是最符合android的架构呢,在国外的论坛里找到了下面的解决办法: The more general problem you are encountering is how to save stateacross several Activities and all parts of your application. A staticvariable (for instance, a singleton) is a common Java way of achievingthis. I have found however, that a more elegant way in Android is toassociate your state with the Application context. --如想在整个应用中使用,在java中一般是使用静态变量,而在android中有个更优雅的方式是使用Application context。 As you know, each Activity is also a Context, which is informationabout its execution environment in the broadest sense. Your applicationalso has a context, and Android guarantees that it will exist as asingle instance across your application. --每个Activity 都是Context,其包含了其运行时的一些状态,android保证了其是single instance的。 下面看一下Demo: class MyApp extends Application { private String myState; public String getState(){ return myState; } public void setState(String s){ myState = s; }}class Blah extends Activity { @Override public void onCreate(Bundle b){ ... MyApp appState = ((MyApp)getApplicationContext()); String state = appState.getState(); ... }}This has essentially the same effect as using a static variable orsingleton, but integrates quite well into the existing Androidframework. Note that this will not work across processes (should yourapp be one of the rare ones that has multiple processes).
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式