1

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?

Marc
  • 16,170
  • 20
  • 76
  • 119
JamieB
  • 703
  • 1
  • 6
  • 17

3 Answers3

2

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.

Community
  • 1
  • 1
Manoj Kumar
  • 1,510
  • 3
  • 20
  • 40
  • 1
    Hmm...this is helpful, and good thinking, though probably less than ideal in my case. If I later create application "E", then A-D must be updated to understand to look for E's preferences. But it would work if A-D were a fixed set and there would never be an "E". I suspect I'm going to just use a file for my solution. – JamieB Nov 02 '12 at 13:46
  • yeah, but make sure that user dont accidently deletes it :P – Manoj Kumar Nov 02 '12 at 13:49
  • Yeah, that's one reason I was hoping to avoid a file. See my last line in the question, too... if there are any other ways to share data between arbitrary apps, I'm open to suggestions! If I read correctly, databases are app-specific and even internal storage is based off of your app's install directory? – JamieB Nov 02 '12 at 13:53
  • The only way i see this through either having a master app if you're going anywhere with either shared prefs or database or have to create a file in asset folder maybe; dig it up and i hope u can conclude well – Manoj Kumar Nov 02 '12 at 14:02
1

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

JamieB
  • 703
  • 1
  • 6
  • 17
0

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.

koljaTM
  • 10,064
  • 2
  • 40
  • 42
  • 1
    Not true -- multiple applications can share SharedPreferences: A writes with Context.MODE_WORLD_READABLE. B reads by using context.createPackageContext("com.whatever.A"... However, this implies that A is always installed. My problem is that none of these is necessarily installed so I have no "master context". – JamieB Nov 02 '12 at 13:35
  • Have to use content provider in complex areas; shared prefs can be shared; search twice before you answer something – Manoj Kumar Nov 02 '12 at 13:35
  • If possible, edit your answer and turn it into some thing useful for the questionare – Manoj Kumar Nov 02 '12 at 14:04