Reputation: 703
Picture, if you will: Applications A, B, C and D. All need to share some small amount of data between them but there is no guarantee that any of them will be installed. That is, you may have B and D only. Or A and C only, etc.
Can SharedPreferences be used in this case?
I understand that I can use createPackageContext to have A-D all use the package space of A, if A is like a master-app that is always installed, but what if I don't know which of these will be installed? createPackageContext returns NameNotFoundException if used on a package space that doesn't exist.
Do I have any content provider options in this case other than a file on the SD card?
Upvotes: 1
Views: 1813
Reputation: 703
Can SharedPreferences be used in this case?
No.
You cannot write SharedPreferences to an arbitrary location. The best you can do is have one central app (like a "loader" or "main menu") which is always installed. Then all other apps can use the central app's preference space to store and share settings (via createPackageContext). But createPackageContext will not work for a package that isn't actually installed (e.g., "com.mystuff.centralstorage" must be an actual installed package, not an arbitrary name)
In this case, the best solution is probably a shared file on the SD card, Java-style. See: FileInputStream, FileOutputStream, DataInputStream and DataOutputStream for handling small, easy text files, as might store preferences. Also, Environment.getExternalStorageDirectory.getAbsolutePath for the path to the SD card's root (that, in fact, is the only Android code necessary for handling files -- the rest can just be Java).
Android ContentProviders seem more geared towards individual apps. There is some sharing permitted (as above, with SharedPerferences) but they seem to embrace the Android "sandbox" concept where every app lives in its own private world, with sharing between them, which is not what's desired here. This solution requires something globally accessible by any number of apps, which a regular old file on the SD card would be.
(Multi-process friendly file protection schemes are another thing to think about.)
Upvotes: 1
Reputation: 10262
To share information between apps you can implement a ContentProvider (or multiple). Or you use a common file on the SDCard. Edit: It seems SharedPreferences can be shared between applications. See above answers.
Upvotes: 0
Reputation: 1520
You can create shared prefs on all four if it is just a small data and check if an app is installed or not before you read its shared pref. How to check app intalled or not? and shared prefs between apps.
Upvotes: 2