From the name alone, I am guessing that Shared Preferences are... shared among apps?
That is, even if my app defines and creates them, any app on android can access them. Is this correct?
(If this isn't correct, then why does the Data Storage Dev Guide emphasize Internal Storage as "Store private data"?)
If this is correct, can I modify this default behavior so that a preference is only visible from the app in which I define and create it?
If so, how do I do that?
SharedPreferences are private by default. They are shared among the components of your app.
If i'm correct you can make store it as private by calling
getSharedPreferences(yourfile, MODE_PRIVATE);
public static final int MODE_PRIVATE
Since: API Level 1
File creation mode: the default mode, where the created file can only be accessed by the calling application (or all applications sharing the same user ID).
See Also:
MODE_WORLD_READABLE
MODE_WORLD_WRITEABLE
Constant Value: 0 (0x00000000)
The getSharedPreferences in the Context class takes two arguments, String name and int mode. Mode determines if the shared preferences are private or not.
Context.getSharedPreferences
Related
I want to get some key-values throughout my application, in iOS we have user-defaults, how can we make preferences behave like that i.e. accessible from any activity?
You can create a new shared preference file or access an existing one by calling one of these methods:
getSharedPreferences() — Use this if you need multiple shared preference files identified by name, which you specify with the first parameter. You can call this from any Context in your app.
getPreferences() — Use this from an Activity if you need to use only one shared preference file for the activity. Because this retrieves a default shared preference file that belongs to the activity, you don't need to supply a name.
For example, the following code accesses the shared preferences file that's identified by the resource string R.string.preference_file_key and opens it using the private mode so the file is accessible by only your app:
Context context = getActivity();
SharedPreferences sharedPref = context.getSharedPreferences(
getString(R.string.preference_file_key), Context.MODE_PRIVATE);
You can read the official guide on Save key-value data.
1) What is the difference between
PreferenceManager.getDefaultSharedPreferences(context)
and
getSharedPreferences(name, mode)
2) And what does each of them do?
3) And how can I make a setting screen (Activity)?
1) You can have multiple SharedPreference files (so they are called SharedPreferences). The argument name of method getSharedPreferences(name, mode) specifies the the name of SharedPreference file to handle.
PreferenceManager.getDefaultSharedPreferences(context) returns the default SharedPreference file having default name and mode. Default name is based on your app's package name (as packagename_preferences.xml) and default mode is MODE_PRIVATE.
If you just want to use a single SharedPreferences file, PreferenceManager.getDefaultSharedPreferences(context) is concise to use.
2) With SharedPreferences you can save some key-value data.
3) Your last question: how to make a setting screen? is too wide topic to answer here. However, I suggest that using PreferenceActivity or PreferenceFragment you can manage a SharedPreferences without handling SharedPreferences directly.
I have a preference screen where I can change some settings.
In the code I can edit them via the shared preferences without a problem.
I have 2 questions:
- do these settings stay saved somewhere when the phone restarts?
- can I insert other settings into the sharedprefs. I mean settings that are not declared in the preference screen.
do these settings stay saved somewhere when the phone restarts?
Yes they are persisted in an xml file on the device.
can I insert other settings into the sharedprefs?
Yes you can have lots of settings in sharedprefs, the only ones that are displayed or modified through the PreferenceActivity are those you set in your layout file and their keys will be the keys specified in the layout file.
As stated before, SharedPreferences are persisted (written into an xml file) so they will always be available even if you restart (so long as you call commit() on the Editor belonging to the SharedPreference).
As far as inserting other settings into the sharedprefs,
You can actually have multiple SharedPreferences (in different files) if you want to be more organized, but you can definitely save anything you want in them.
To get the default shared preference for the activities context:
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
To get one of many SharedPreference that you created, use this:
SharedPreferences prefs = context.getSharedPreferences(String name, int mode)
name being the name of the file
mode being MODE_PRIVATE, MODE_WORLD_READABLE, MODE_WORLD_WRITEABLE, MODE_MULTI_PROCESS depending on how you want other applications to be able to access your apps prefs which can be useful
I just curious. There are 3 method:
1. getPreferenceManager().setSharedPreferencesName(String PrefName);
2. PreferenceManager.getDefaultSharedPreferences(Context context)
3. Context.getSharedPreferences (String name, int mode)
As I know, the third method is only used when the first method is used, right?
But with 3 method we also use addPreferencesFromResource(int resID);
so, what is the difference? When can we use one of these method?
Thanks!
Let's go one step at a time:
setSharedPreferencesName() is method that allows to set the name of the preference group for later use. This is helpful for example when using the helper class of
PreferencesActivity before loading a preferences from XML resource file by calling addPreferencesFromResource(). It is therefore not as common as the other 2 methods you mentioned above.
getDefaultSharedPreferences() uses a default name, usually stored as /data/data/com.package.name/shared_prefs/com.package.name_preferences.xml.
It is commonly used. Note that this default is set per application.
The alternative method - getSharedPreferences() requires to indicate a specific preference (file) name and an operation mode.
As appears also in another answer about shared preferences,
getDefaultSharedPreferences() in fact uses Context.getSharedPreferences, so the result is the same, but without the flexbility to split to multiple preference files, that is offered by getSharedPreferences(). Sharing the preferences between apps using
a MODE_WORLD_READABLE operation indicator is also something possible using getSharedPreferences(), but is rarely used.
IMHO, getDefaultSharedPreferences() can be safely used without going into the confusion of multiple preference file names that are prone to typos and confusion.
If someone knows of a good reason to use getSharedPreferences() and not getDefaultSharedPreferences(), please let me know by commenting here.
getDefaultSharedPreferences() uses a default preference-file name like "com.example.something_preferences". This default is set per application, so all activities in the same app context can access it easily as in the following example:
SharedPreferences spref = PreferenceManager.getDefaultSharedPreferences(this);
if (spref.contains("email")) {
String sEmailAddr = spref.getString("email", "");
}
The preferences are usually stored at /data/data/com.package.name/shared_prefs/com.package.name_preferences.xml
getSharedPreference is the best way because using getDefaultSharedPreferences has some flaws
Actualy getDefaultSharedPreferences doesn't work correct on some
devices when build with targer api 13
Starting app from shortcut and from menu gives me different
DefaultSharedPreferences. After removing DefaultSharedPreferences
from my code - it works perfect. I can't just say: people dont make
shrotcuts, so I had to change code
This link may also help
In my app, I use a PreferenceActivity framework to store persistent data. My intent is to create multiple save files, all of which may be accessed by the Preferences, but only one at a time.
When is it better to use a private file generated by Context.openFileOutput() and when is it better to use SharedPreferences?
EDIT
My data exists solely in primitives.
Normally developers use a preference file that is common to an entire app using getDefaultSharedPreferences.
However, Android has a getSharedPreferences(String name, int mode) method in Context. You could use this to have multiple preference files, in your case - save files, by using unique names passed into the name parameter.
Regarding volatility, you can force the preferences to save by getting an Editor via edit() and then calling commit().
Make sure to note that the SharedPreferences will indeed be shared based on the name:
Retrieve and hold the contents of the preferences file 'name', returning a SharedPreferences through which you can retrieve and modify its values. Only one instance of the SharedPreferences object is returned to any callers for the same name, meaning they will see each other's edits as soon as they are made.