SagiLow
SagiLow

Reputation: 6029

How to find my sharedPreferences xml file for use of getSharedPreferences() in Android

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 ...

Upvotes: 0

Views: 3634

Answers (3)

JJ86
JJ86

Reputation: 5113

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");

Upvotes: 0

Booger
Booger

Reputation: 18725

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

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());

Upvotes: 4

Hoan Nguyen
Hoan Nguyen

Reputation: 18151

SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context);

Upvotes: 2

Related Questions