// set
((MyApplication) this.getApplication()).setSomeVariable("foo");
// get
String s = ((MyApplication) this.getApplication()).getSomeVariable();
12
您可以像这样使用 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;
}
}
在您的应用程序中,您可以通过以下方式访问单身人士:
MyProperties.getInstance().someValueIWantToKeep
-6
这个全局变量适用于我的项目:
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;
1
您可以通过几种不同的方式实现您的要求 .
1.) Extend the application class and instantiate your controller and model objects there.
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;
}
}
然后,您可以随时从自定义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);
}
2.) You can have your controller just create a singleton instance of itself:
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;
}
}
3.) Finally, there is a slick framework created at Square which provides you dependency injection within Android. It is called Dagger. I won't go into how to use it here, but it is very slick if you need that sort of thing.
我希望我能详细说明你如何做你所希望的 .
477
尝试这样:
创建共享数据类:
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;
}
}
public interface ActivityClass {
public static final String MYSTRING_1 = "STRING";
public static final int MYINT_1 = 1;
}
然后,您可以使用以下内容访问类中的任何位置:
int myInt = ActivityClass.MYINT_1;
String myString = ActivityClass.MYSTRING_1;
2
// My Class Global Variables Save File Global.Java
public class Global {
public static int myVi;
public static String myVs;
}
// How to used on many Activity
Global.myVi = 12;
Global.myVs = "my number";
........
........
........
int i;
int s;
i = Global.myVi;
s = Global.myVs + " is " + Global.myVi;
6
您可以像这样创建一个 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;
}
}
14 回答
您可以使用应用程序首选项只要您传递Context对象,它们就可以从任何活动或代码片段访问,并且它们对使用它们的应用程序是私有的,因此您不必担心暴露应用程序特定值,除非您处理路由设备 . 即便如此,您也可以使用散列或加密方案来保存值 . 此外,这些首选项从应用程序运行存储到下一个 . Here是您可以查看的一些代码示例 .
您可以扩展基础
android.app.Application
类并添加成员变量,如下所示:在您的Android清单中,您必须声明实现android.app.Application的类(将
android:name=".MyApplication"
属性添加到现有的应用程序标记):然后在您的活动中,您可以获取并设置变量,如下所示:
您可以像这样使用
Singleton Pattern
:在您的应用程序中,您可以通过以下方式访问单身人士:
这个全局变量适用于我的项目:
您可以通过几种不同的方式实现您的要求 .
1.) Extend the application class and instantiate your controller and model objects there.
然后,您可以随时从自定义Application对象中调用您的单例:
2.) You can have your controller just create a singleton instance of itself:
然后你可以从任何Activity或Fragment调用create方法,如果还没有,它将创建一个新的控制器,否则它将返回预先存在的控制器 .
3.) Finally, there is a slick framework created at Square which provides you dependency injection within Android. It is called Dagger. I won't go into how to use it here, but it is very slick if you need that sort of thing.
我希望我能详细说明你如何做你所希望的 .
尝试这样:
创建共享数据类:
SharedData.java
在要设置/获取数据的那些类中声明/初始化类的实例(在
onCreate()
方法之前使用此代码): -设定数据:
获取数据:
我检查了类似的答案,但这里给出的那些不符合我的需要 . 从我的观点来看,我发现了一些你正在寻找的东西 . 唯一可能的黑点是安全问题(或许不是),因为我不了解安全性 .
我建议使用Interface(不需要使用带有构造函数的Class等等),因为你只需创建类似的东西:
然后,您可以使用以下内容访问类中的任何位置:
您可以像这样创建一个
Global Class
:然后在清单中定义它:
现在您可以将值设置为全局变量,如下所示:
你可以得到这样的值:
请从这个博客中找到完整的例子Global Variables
从技术上讲,这不回答问题,但我建议使用Room数据库而不是任何全局变量 . https://developer.android.com/topic/libraries/architecture/room.html即使你是'only'需要存储一个全局变量并且没什么大不了的,但是使用Room数据库是保持活动生命周期中值的最优雅,原生且受到良好支持的方式 . 它将有助于防止许多问题,尤其是数据的完整性 . 我理解数据库和全局变量是不同的,但为了代码维护,应用程序稳定性和数据完整性,请使用Room .
如果可能的话,你应该声明你需要保持的变量,这些变量还没有被垃圾收集器清除或者在文件中由OS卸载 . 所以要做到这一点,你必须用C / C编译并编译成.so lib文件和将其加载到您的MainActivity中 .
使用SharedPreferences存储和检索全局变量 .
简单!!!!
您想要作为全局变量访问的变量,可以将它们声明为静态变量 . 现在,您可以使用以下方式访问这些变量
classname.variablename;
}
MyProperites.someValueIWantToKeep;
而已! ;)