Jef*_*elt 510
您可以扩展基android.app.Application类并添加成员变量,如下所示:
public class MyApplication extends Application {
private String someVariable;
public String getSomeVariable() {
return someVariable;
}
public void setSomeVariable(String someVariable) {
this.someVariable = someVariable;
}
}
Run Code Online (Sandbox Code Playgroud)
在您的Android清单中,您必须声明实现android.app.Application的类(将该android:name=".MyApplication"属性添加到现有的应用程序标记):
<application
android:name=".MyApplication"
android:icon="@drawable/icon"
android:label="@string/app_name">
Run Code Online (Sandbox Code Playgroud)
然后在您的活动中,您可以获取并设置变量,如下所示:
// set
((MyApplication) this.getApplication()).setSomeVariable("foo");
// get
String s = ((MyApplication) this.getApplication()).getSomeVariable();
Run Code Online (Sandbox Code Playgroud)
Ram*_*mps 41
你可以使用Singleton Pattern这样的:
package com.ramps;
public class MyProperties {
private static MyProperties mInstance= null;
public int someValueIWantToKeep;
protected MyProperties(){}
public static synchronized MyProperties getInstance() {
if(null == mInstance){
mInstance = new MyProperties();
}
return mInstance;
}
}
Run Code Online (Sandbox Code Playgroud)
在您的应用程序中,您可以通过以下方式访问单身人士:
MyProperties.getInstance().someValueIWantToKeep
Run Code Online (Sandbox Code Playgroud)
小智 13
这个全局变量适用于我的项目:
public class Global {
public static int ivar1, ivar2;
public static String svar1, svar2;
public static int[] myarray1 = new int[10];
}
// How to use other or many activity
Global.ivar1 = 10;
int i = Global.ivar1;
Run Code Online (Sandbox Code Playgroud)
小智 7
您可以通过几种不同的方式实现您的要求.
1.)扩展应用程序类并在那里实例化控制器和模型对象.
public class FavoriteColorsApplication extends Application {
private static FavoriteColorsApplication application;
private FavoriteColorsService service;
public FavoriteColorsApplication getInstance() {
return application;
}
@Override
public void onCreate() {
super.onCreate();
application = this;
application.initialize();
}
private void initialize() {
service = new FavoriteColorsService();
}
public FavoriteColorsService getService() {
return service;
}
}
Run Code Online (Sandbox Code Playgroud)
然后,您可以随时从自定义Application对象中调用您的单例:
public class FavoriteColorsActivity extends Activity {
private FavoriteColorsService service = null;
private ArrayAdapter<String> adapter;
private List<String> favoriteColors = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_favorite_colors);
service = ((FavoriteColorsApplication) getApplication()).getService();
favoriteColors = service.findAllColors();
ListView lv = (ListView) findViewById(R.id.favoriteColorsListView);
adapter = new ArrayAdapter<String>(this, R.layout.favorite_colors_list_item,
favoriteColors);
lv.setAdapter(adapter);
}
Run Code Online (Sandbox Code Playgroud)
2.)您可以让您的控制器只创建自己的单例实例:
public class Controller {
private static final String TAG = "Controller";
private static sController sController;
private Dao mDao;
private Controller() {
mDao = new Dao();
}
public static Controller create() {
if (sController == null) {
sController = new Controller();
}
return sController;
}
}
Run Code Online (Sandbox Code Playgroud)
然后你可以从任何Activity或Fragment调用create方法,如果还没有,它将创建一个新的控制器,否则它将返回预先存在的控制器.
3.)最后,在Square上创建了一个光滑的框架,它为您提供Android内的依赖注入.它被称为Dagger.我不会在这里讨论如何使用它,但如果你需要那种东西它会很光滑.
我希望我能详细说明你如何做你所希望的.
使用 SharedPreferences 来存储和检索全局变量。
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
String userid = preferences.getString("userid", null);
Run Code Online (Sandbox Code Playgroud)
你可以创建一个Global Class这样的:
public class GlobalClass extends Application{
private String name;
private String email;
public String getName() {
return name;
}
public void setName(String aName) {
name = aName;
}
public String getEmail() {
return email;
}
public void setEmail(String aEmail) {
email = aEmail;
}
}
Run Code Online (Sandbox Code Playgroud)
然后在清单中定义它:
<application
android:name="com.example.globalvariable.GlobalClass" ....
Run Code Online (Sandbox Code Playgroud)
现在您可以像这样将值设置为全局变量:
final GlobalClass globalVariable = (GlobalClass) getApplicationContext();
globalVariable.setName("Android Example context variable");
Run Code Online (Sandbox Code Playgroud)
您可以像这样获得这些值:
final GlobalClass globalVariable = (GlobalClass) getApplicationContext();
final String name = globalVariable.getName();
Run Code Online (Sandbox Code Playgroud)
请从这个博客全局变量中找到完整的例子
像这样尝试:
创建一个共享数据类:
SharedData.java
import android.app.Application;
/**
* Created by kundan on 6/23/2015.
*/
public class Globals {
private static Globals instance = new Globals();
// Getter-Setters
public static Globals getInstance() {
return instance;
}
public static void setInstance(Globals instance) {
Globals.instance = instance;
}
private String notification_index;
private Globals() {
}
public String getValue() {
return notification_index;
}
public void setValue(String notification_index) {
this.notification_index = notification_index;
}
}
Run Code Online (Sandbox Code Playgroud)
在要设置/获取数据的类中全局声明/初始化类的实例(在onCreate()方法前使用此代码):
Globals sharedData = Globals.getInstance();
Run Code Online (Sandbox Code Playgroud)
设置数据:
sharedData.setValue("kundan");
Run Code Online (Sandbox Code Playgroud)
获取数据:
String n = sharedData.getValue();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
338519 次 |
| 最近记录: |