简体   繁体   English

Android-具有两个类的共享首选项

[英]Android - Shared Preference with Two Classes

Basically I want to set up one class to have getters and setters to the device to store and retrieve data, and other classes to access it. 基本上,我想设置一个类,使其具有用于设备的getter和setter来存储和检索数据,以及用于访问它的其他类。 I managed to get sharedpreferences working in one class but having trouble with two classes (I'm familiar with Java but not Android, I read somewhere I shouldn't be using Activity but static, and couldn't get that working either). 我设法使sharedpreferences在一个类中工作,但在两个类中遇到了麻烦(我熟悉Java但不熟悉Android,我读过某个地方,我不应该使用Activity,而是静态的,也不能正常工作)。 Anyway here is the getter / setter class. 无论如何,这里是getter / setter类。

public class Storage extends Activity {


@Override
public void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
}

final SharedPreferences prefs = this.getSharedPreferences(
        "uk.co.kenreid.examplestory", Context.MODE_PRIVATE);

String nameKey = "uk.co.kenreid.examplestory.name";

I access it with these pieces of code in the other class (final is used as the variable "storage" is used within an onclicklistener): 我在其他类中使用以下代码段进行访问(最终用作onclicklistener中的变量“ storage”):

final Storage storage = new Storage();

storage.storeItem("name", name);
System.out.println(storage.getString("name"));

尝试这个:

SharedPreferences prefs = this.getSharedPreferences("uk.co.kenreid.examplestory", Context.MODE_MULTI_PROCESS);

Storing something in shared preference: 以共享首选项存储内容:

    SharedPreferences prefs = getSharedPreferences("TOT", Context.MODE_PRIVATE);  
    Editor ed = prefs.edit();  
    ed.putString(key, value);  
    ed.commit(); 

In your case you could use the above code inside the onCreate method. 您可以在onCreate方法中使用上面的代码。

Fetching something from shared preference: 从共享的偏好中获取一些东西:

    SharedPreferences prefs = getSharedPreferences("TOT", Context.MODE_PRIVATE);
    prefs.getString(key, null);

I believe that your issue is initialising the SharedPreferences away from the Activity ? 我相信您的问题是要从Activity初始化SharedPreferences吗?

Assuming this is your issue, you can pass a Context through the constructor of the Storage class to initalise it. 假设这是您的问题,则可以通过Storage类的构造函数传递一个Context来初始化它。

Eg. 例如。

SharedPreferences prefs;

public Storage(Context aContext){
    prefs = aContext.getSharedPreferences("uk.co.kenreid.examplestory", 
            Context.MODE_PRIVATE);
}

Then access via your original acessor methods. 然后通过原始的acessor方法进行访问。 Might be best to pass the Application Contex t rather than Activity Context through to the constructor, so when you create your Storage class, call it like this in the Activity: 最好是将Application Contex t而不是Activity Context传递给构造函数,因此在创建Storage类时,请在Activity中这样调用它:

Storage myStorage = new Storage(getApplicationContext());

It might be wise to create a Singleton class as well, to allow you to use the same instance of Storage. 同样,最好创建一个Singleton类,以允许您使用相同的Storage实例。 There are plenty of tutorials for Singletons in Java around the web. 网上有很多Java单例教程。

Try like this: 尝试这样:

SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(getActivity());

mVariable = sharedPref.getBoolean(SettingsActivity.KEY_TO_VARIABLE, false);

check the doc of getSharedPreferences 检查getSharedPreferences的文档

Here the first parameter is the namem of the Shared Preference where you are storing the data. 在这里,第一个参数是存储数据的共享首选项的名称。 So you need to use the same name from every classes to access the same shared preferences. 因此,您需要在每个类中使用相同的名称来访问相同的共享首选项。

You can use a separate class can handle all your preferences 您可以使用单独的类可以处理您的所有首选项

public class PreferenceManager {
    public static void setLogInSuceess(boolean login, Context context) {
        SharedPreferences preferences;
        preferences = context.getSharedPreferences("settings", Context.MODE_PRIVATE);

        preferences.edit().putBoolean("LOGIN", login).commit();
    }

    public static boolean getLogInSuceess(Context context){
        SharedPreferences preferences;
        preferences = context.getSharedPreferences("settings", Context.MODE_PRIVATE);
        boolean login = preferences.getBoolean("LOGIN", false);

        return login;
    }
}

Use it where ever you want, 随时随地使用它,

PreferenceManager.setLogInSuceess(true/false, context); PreferenceManager.setLogInSuceess(true / false,context); boolean _login = PreferenceManager.getLogInSuceess(context); boolean _login = PreferenceManager.getLogInSuceess(context);

Try this: 尝试这个:

public class Storage extends Application

Application is a class used to share data from Activities 应用程序是用于共享活动数据的类

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM