简体   繁体   中英

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. 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). Anyway here is the getter / setter class.

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):

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.

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 ?

Assuming this is your issue, you can pass a Context through the constructor of the Storage class to initalise it.

Eg.

SharedPreferences prefs;

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

Then access via your original acessor methods. 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:

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. There are plenty of tutorials for Singletons in Java around the web.

Try like this:

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

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

check the doc of 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); boolean _login = PreferenceManager.getLogInSuceess(context);

Try this:

public class Storage extends Application

Application is a class used to share data from Activities

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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