How to create sharedpref shared by few apps? - android

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

Related

Access data from other application saved in preference

If an application has saved data using SharedPreference like this:
SharedPreferences.Editor preferencesEditor = getDefaultSharedPreferences.edit();
preferencesEditor.putInt("count", mValue);
preferencesEditor.apply();
can an other application have access to these data after reboot like this for example:
SharedPreferences sharedPref = getDefaultSharedPreferences();
int value = sharedPref.getInt("count",0);
And if it is not posssible, how is possible from an application to store variable so that other application have access to it?
Yes it is possible, but both apps need to be signed by the same certificates among other things, check this answer for details.
An application can also share and access data from a public content provider and this is usually the recommended way of sharing data among different applications.
You can also have a shared file in storage that both apps can read and write.

Multi-process safe sharedprefs

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.

ContentProvider to expose app preferences

I want to write a content provider for an app that exposes some data sored in the app's shared preferences. Is it possible to do that?
Why do you want to to do that ? Normally Preferences are secret data which should be only available to source app.
Incase you want to do that, you can read other's app preferences by obtaining the context of source application
Context otherAppsContext = createPackageContext("other.application.package", mode);
Also you should understand that you can’t get all preferences. When you will receive results you will see that PRIVATE and WORLD_WRITABLE aren’t available.

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

Android Default Shared Preferences File Readable By Multiple Apps?

How can I have a shared preferences file that can be read by more than one app. In particular by an admin app and the app it is administrating?
Have you tried getSharedPreferences() with MODE_WORLD_WRITEABLE (assuming that both apps are supposed to be able to write to it)? One of the apps will have to create a context using the package of the other app.

Categories

Resources