Multi-process safe sharedprefs - android

Is it safe to use SharedPreferences if I have 2 processes in my app and I will create 2 separate preference files? I mean 1 file for main process and 2nd file for 2nd process. And the 2 processes wouldn't access other than their own file?
For creating separate files I would use:
Context.getSharedPreferences(String name, int mode), where name is name of the file.

So long as you are careful and do not accidentally try using the same SharedPreferences on both processes, what you describe should be OK.
Personally, I would consider using some other form of file (e.g., JSON, SQLite), as I'm not certain what the advantage is of using SharedPreferences here.

Related

Save SharedPreferences file for backup purposes

I want to save user progress in my game which is essentially all saved in a sharedprefrences file.
So, instead of using third party services, I am wondering if I can simply save the whole file on my server
and in time just pull it and apply it locally?
So the question is - can a sharedprefrences file be saved as a bulk?
for saving progress locally you can use SQLite database or Room Database so you can easily update in the future if you want to update ... I will recommend room database rather than sqlite3 because that is consuming more time and room database easily you can work on crud operation without taking too much time ...and sharedpreference for getting issue if you want to update value and more time to take your job and confusion so use room database...easy and less time
Usually, you can find SharedPreferences xml file in /data/data/<your.package.name>/shared_prefs/ directory. But it depends on manufacter, so it's not guaranteed.
(For more information see this answer).
For getting shared pref file Context has folowing methods - new File getSharedPreferencesPath(String name); and depercated File getSharedPrefsFile(String name);
(According to source )
You can use one of them(or both for different Android versions) via reflection.
But, actually, it's not reccomended approach. Better choice is write your own prefs synchronization by copying all values with all keys.
Or just store preferences in your own database.

Does SharedPreferences name need to be unique?

Probably a dumb question, but I"m using a SharedPreference with a few different names "MyPrefs1", "MyPrefs2" etc.
I'm assuming this is restricted to my app. i.e. if some other app tries to use the same name, it won't overwrite my values
I pretty much believe my understanding is correct, but the name "SharedPreferences" seem to indicate that it can be shared between apps? (is it for Sharing between activities?)
That's correct, the SharedPreferences are stored in your app's private folder (to be exact, in /data/data/your package name/shared_prefs).
You can give them whatever name you want.
SharedPreferences represent the preferences which can be shared between different components of your application. The SharedPreferences you create in your application is never exposed with other applications.
Whether you use PreferenceManager.getDefaultSharedPreferences() or Context.getSharedPreferences("file_name", Context.MODE_PRIVATE); both are particular to your application only.
Note - SharedPreferences or Preferences is not exposed to other applications.
Although you assumption is right that the SharedPreferences are not shared between apps and thus names cannot clash, it's a good practice (besides of put a specific name to your shared preferences file) to add the package of your application as a prefix. This is useful if you have different flavours and you don't want to overwrite your preferences among them, especially when you're testing an app and don't want to screw up the original values of the shared preferences files.

How to create sharedpref shared by few apps?

I have 4-5 different apps and I want to keep their settings parameters common. I want to do it through sharedpref.
Please suggest how to share sharedpref among different apps without making it public for all app.
using following function:
getSharedPreference(String name, int mode)
retrieves and holds content of file path with particular name.
MODE_PRIVATE: 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).
MODE_WORLD_READABLE: File creation mode: allow all other applications
to have read access to the created file.
MODE_WORLD_WRITEABLE : File creation mode: allow all other
applications to have write access to the created file.
To access World Readable/Writeable from different app:
Context myContext = createPackageContext("com.example.app",
Context.MODE_WORLD_READABLE); // where com.example.app is the app containing the preferences
SharedPreferences testPrefs = myContext.getSharedPreferences
("test_prefs", Context.MODE_WORLD_READABLE); //test_prefs is the name in getSharePreference's argument
You could create a service that will be used by those applications, it will be managing your preferences. For service you can specify which applications can use it.
Use same key to read and write shared preference in all your apps you will get the same result as you get in app1 and in app2 ...

SharedPreferences: What are they shared with?

I find the name of stored preferences on Android (SharedPreferences) to be a tad confusing. It was probably chosen with a purpose, so how exactly are these preferences shared? e.g. I don't want other applications accessing my app's information willy-nilly.
What is the difference between preferences from getPreferences() and preferences from getSharedPreferences(), and what is the difference between the different modes? How should I choose which to use? (Heck, if I have a multi-activity program, will using just the regular getPreferences be detrimental?)
My imagination regarding the use of these things is still pretty limited.
It was probably chosen with a purpose, so how exactly are these preferences shared?
They are shared among all components of your application (e.g., all of your activities).
e.g. I don't want other applications accessing my app's information willy-nilly.
SharedPreferences are private to your application by default.
What is the difference between preferences from getPreferences() and preferences from getSharedPreferences(), and what is the difference between the different modes? How should I choose which to use? (Heck, if I have a multi-activity program, will using just the regular getPreferences be detrimental?)
I am not quite certain what you are referring to, since you provided just bare method names with no classes. If you are intending on collecting preference values from the user via the preference screen system, use PreferenceManager.getDefaultSharedPreferences(). I generally use that for everything.
Sharedprefernce are basically key,value pairs with can are saved in an xml and is accessible by an application. You can use it to save some setting value or default values or any other form of key value pairs.
So a key,value pair can be saved in one activity can be accessed in other activity.
Different modes means that whether the data saved can be used by other application or not. You should not worry about all modes. As MODE_WORLD_READABLE & MODE_WORLD_WRITEABLE are deprecated now.
For the difference:-
Difference between getShared/get preference

SharedPreferences : Why are they called so?

As per my understanding the values stored using the SharedPreferences in android can be used only by the app which created it.(correct me if wrong).
Then what is the significance of the name SharedPreferences.
Is something really shared here?
well it can be shared accross applications if you set the mode to WORLD READABLE or WORLD_WRITEABLE. (think ringtone settings, notification message etc...)
see this link
public static final int MODE_WORLD_READABLE
Since: API Level 1
File creation mode: allow all other applications to have read access to
the created file.
The SharedPreferences use a secure /data directory for preferences that are shared within your app's activities and services.
http://developer.android.com/reference/android/content/SharedPreferences.html

Categories

Resources