在Android中如何使用全局变量--Application context (转)
推荐于2018-05-15 · 知道合伙人软件行家
关注
展开全部
可以将变量存放在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中读取全局变量。
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中读取全局变量。
展开全部
做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).
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询