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的。
The way to do this is to create your own subclass of android.app.Application,and then specify that class in the application tag in your manifest.Now Android will automatically create an instance of that class andmake it available for your entire application. You can access it fromany context using the Context.getApplicationContext() method (Activityalso provides a method getApplication() which has the exact sameeffect):
–方法是创建一个属于你自己的android.app.Application的子类,然后在manifest中申明一下这个类,这是 android就为此建立一个全局可用的实例,你可以在其他任何地方使用Context.getApplicationContext()方法获取这个实例,进而获取其中的状态(变量)。

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).
–这个效果就是使用静态变量是一样的,但是其更符合android的架构体系。

 

原文链接:http://stackoverflow.com/questions/708012/android-how-to-declare-global-variables

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android 定义全局变量有以下几种方法: 1. 在 Application 类定义全局变量 可以在自定义的 Application 类定义全局变量,这样在整个应用程序都可以访问到该变量。具体的实现步骤如下: - 创建一个继承自 Application 类的自定义类; - 在自定义定义一个静态变量; - 在 AndroidManifest.xml 文件指定自定义类。 示例代码如下: ``` public class MyApplication extends Application { public static int globalVar = 0; } ``` 可以在任何地方通过 `MyApplication.globalVar` 来访问该全局变量。 2. 使用 SharedPreferences 存储全局变量 SharedPreferences 是一种轻量级的数据存储方式,可以用来存储一些简单的全局变量。具体的实现步骤如下: - 获取 SharedPreferences 对象; - 使用 SharedPreferences.Editor 对象保存全局变量; - 在需要访问全局变量的地方获取 SharedPreferences 对象,并通过 key 值获取全局变量。 示例代码如下: ``` public class MyApplication extends Application { private SharedPreferences sharedPreferences; @Override public void onCreate() { super.onCreate(); sharedPreferences = getSharedPreferences("globalVar", Context.MODE_PRIVATE); SharedPreferences.Editor editor = sharedPreferences.edit(); editor.putInt("var1", 0); editor.apply(); } public int getGlobalVar() { return sharedPreferences.getInt("var1", 0); } public void setGlobalVar(int var) { SharedPreferences.Editor editor = sharedPreferences.edit(); editor.putInt("var1", var); editor.apply(); } } ``` 可以在任何地方通过 `MyApplication.getInstance().getGlobalVar()` 和 `MyApplication.getInstance().setGlobalVar(var)` 来访问和修改该全局变量
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值