0

I'm trying to have a checkbox in preferences to check if user wants the application to run on boot.
The settings file is called settings.xml in is under PROJECT.res.xml.setting.xml.
But when I try to use this file to read the checkbox on my Receiver, I can't find the setting.xml file.
I've tried the following combinations:

SharedPreferences sharedPrefs = context.getSharedPreferences("R.res.xml.settings.xml", Context.MODE_PRIVATE);
SharedPreferences sharedPrefs = context.getSharedPreferences("R.xml.settings.xml", Context.MODE_PRIVATE);
SharedPreferences sharedPrefs = context.getSharedPreferences(".settings", Context.MODE_PRIVATE);
SharedPreferences sharedPrefs = context.getSharedPreferences("settings", Context.MODE_PRIVATE);

I really don't know what is the correct way ...

4

3 回答 3

4

This is the way I get SharedPreferences (notice I don't give it a XML file name):

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
于 2013-03-30T17:04:25.353 回答
2
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context);
于 2013-03-30T17:04:03.863 回答
0

I use this code for saving a String on SharedPreferences:

    SharedPreferences prefs = this.getSharedPreferences("your.project.app", Context.MODE_PRIVATE);
    String stringToSave = "your.project.app.nameOfString";
    prefs.edit().putString(stringToSave, "string content").commit();

And if you want to open it later use this:

    SharedPreferences prefs = this.getSharedPreferences("your.project.app", Context.MODE_PRIVATE);
    String savedString = "your.project.app.nameOfString";
    String content = prefs.getString(savedString, "none");
于 2013-03-30T17:08:00.663 回答