如何在应用程序的生命周期中创建全局变量,而不管哪个活动正在运行。


当前回答

我检查了类似的答案,但这里给出的不符合我的需要。 我找到了,在我看来,正是你要找的东西。唯一可能的黑点是安全问题(也可能不是),因为我不了解安全问题。

我建议使用Interface(不需要使用带有构造函数的Class,所以…),因为你只需要创建如下内容:

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;

其他回答

试着这样做:

创建共享数据类:

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;
    }



}

在那些你想要设置/获取数据的类中声明/初始化一个类的全局实例(在onCreate()方法之前使用此代码)

Globals sharedData = Globals.getInstance();

组数据:

sharedData.setValue("kundan");

得到的数据:

String n = sharedData.getValue();

简单! !

那些你想作为全局变量访问的变量,你可以声明为静态变量。现在,你可以通过

classname.variablename;

public class MyProperties {
private static MyProperties mInstance= null;

static int someValueIWantToKeep;

protected MyProperties(){}

public static synchronized MyProperties getInstance(){
    if(null == mInstance){
        mInstance = new MyProperties();
    }
    return mInstance;
}

}

MyProperites.someValueIWantToKeep;

Thats It !;)

你可以像这样创建一个全局类:

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;
    }
}

然后在manifest中定义它:

<application
    android:name="com.example.globalvariable.GlobalClass" ....

现在你可以像这样设置全局变量的值:

final GlobalClass globalVariable = (GlobalClass) getApplicationContext();
globalVariable.setName("Android Example context variable");

你可以像这样得到这些值:

final GlobalClass globalVariable = (GlobalClass) getApplicationContext();
final String name  = globalVariable.getName();

请从这个博客中找到完整的例子 全局变量

Technically this does not answer the question, but I would recommend using the Room database instead of any global variable. https://developer.android.com/topic/libraries/architecture/room.html Even if you are 'only' needing to store a global variable and it's no big deal and what not, but using the Room database is the most elegant, native and well supported way of keeping values around the life cycle of the activity. It will help to prevent many issues, especially integrity of data. I understand that database and global variable are different but please use Room for the sake of code maintenance, app stability and data integrity.

您可以使用应用程序首选项。只要您传递Context对象,就可以从任何活动或代码段访问它们,并且它们对于使用它们的应用程序是私有的,因此您不需要担心暴露应用程序特定的值,除非您处理路由设备。即使如此,您也可以使用哈希或加密方案来保存值。此外,这些首选项将从一个应用程序运行到下一个应用程序运行。 下面是您可以参考的一些代码示例。