11

I'm developing a group of complex Android applications that need to share common state and configuration settings.

For example, see this picture explaining my scenario:

enter image description here

I want that APP 1, APP 2 and APP 3 be able to access (read/write) data to the common storage area. Additionally, I need uninstall protection i.e. I don't want the data to be removed when the user uninstalls any of the apps.

I've already read about SQLite databases, ContentProviders and writing on Internal and External storage, but each of the above mentioned methods have disadvantages as listed below:

  • SQLite database: DB is deleted on app uninstall and is private to each app
  • ContentProvider: The data is removed when the app with the ContentProvider is removed
  • Internal storage: Is private to each app and data is deleted on app uninstall (http://developer.android.com/training/basics/data-storage/files.html#InternalVsExternalStorage)
  • External storage: Is unreliable (user may remove SD card)
  • Store on server: Not possible, user may not have reliable internet connection

EDIT:

I don't want any dependencies on Google Play Services because I will be distributing the apps via Play Store and as 3rd party downloads.

Please help me out.

jazdev
  • 486
  • 3
  • 14
  • 1
    `External storage: Is unreliable (user may remove SD card)`. Wrong idea. On most devices external memory is build in. If you add a micro SD card then that is removable memory but often named external memory. – greenapps Sep 12 '14 at 16:28
  • Were you able to solve this problem? – dariru Jun 20 '18 at 06:38
  • Did you find a solution to this problem ? – Walid Jan 22 '21 at 13:41

4 Answers4

1

Google Drive sort of does this for you. You basically get granted permission to a local filesystem that is backed by a remote one. On most phones this is getting preinstalled so the uninstall issue you are worried about is less of an issue.

You could create a folder for your apps that you can then read/write.

https://developers.google.com/drive/android/

Greg Giacovelli
  • 10,164
  • 2
  • 47
  • 64
  • But what if the user doesn't have Play Services installed? This approach fails in that case. – jazdev Sep 12 '14 at 16:27
  • If you are targeting markets of the Google Play Store (this covers more than majority of the number of phones out there) then you can have them install the google play services(APi call). If you are worried about that being an issue, I recommend getting really good at bizdev to have your remote filesystem app preinstalled in the system partition by all the large cell phone manufacturers, so that normal users can't remove it from their phone ;) And then you can satisfy all your requirements. The uninstall proof requirement sort of goes against the app model of removing the app and data as one. – Greg Giacovelli Sep 12 '14 at 16:32
1

You can use the shared preferences object to read and write preferences data from a file. Most important is to use MODE_MULTI_PROCESS. The bit MODE_MULTI_PROCESS is used if multiple processes are mutating the same SharedPreferences file.

Use the following code:

SharedPreferences shPrefernces = context.getSharedPreferences("filename", MODE_MULTI_PROCESS);
String s1 = shPrefernces.getString("keytosearch1", "");
String pass = shPrefernces.getString("keytosearch2", "");
Nipun Anand
  • 479
  • 2
  • 6
  • And where would this file reside? – greenapps Sep 12 '14 at 16:28
  • SharedPreferences are stored in an xml file in the app data folder, i.e. /data/data/YOUR_PACKAGE_NAME/shared_prefs/YOUR_PREFS_NAME.xml or the default preferences at: /data/data/YOUR_PACKAGE_NAME/shared_prefs/YOUR_PACKAGE_NAME_preferences.xml SharedPreferences added during runtime are not stored in the Eclipse project. – Nipun Anand Sep 12 '14 at 16:30
  • Is it safe to save sensitive data using MODE_MULTI_PROCESS? – jazdev Sep 12 '14 at 16:32
  • So then this will not help the OP as the other apps cannot reach it. – greenapps Sep 12 '14 at 16:32
  • 1
    This flag is not related to security, but to access concurency. If you go through the documentation it suggests to use MODE_MULTI_PROCESS for sharing preferences across multiple processes. Using Context.getSharedPreferences(...) every time you want to write or read into preferences is not process-safe either. – Nipun Anand Sep 12 '14 at 16:49
  • shared preferences files are stored in the data folder. Please follow the link below for more details: http://developer.android.com/guide/topics/data/data-storage.html#pref – Nipun Anand Sep 12 '14 at 16:59
  • 1
    Yeah Data folders get deleted when the app is uninstalled. So this will not work. The Multiprocess flag only makes sure that processes with access credentials to a shared preference, always see the same state when reading from a SharedPreferences instance. It's actually slower since you have to check the disk on every access and doesn't solve any of the OP's criteria. Unless you use WORLD_WRITEABLE WORLD_READABLE in which case everyone can read and write it, but I doubt that's what is really desired – Greg Giacovelli Sep 14 '14 at 03:16
  • Actually what you are trying to do is not officially supported, although there may be a supported way to do this which as per android documentation will be added to Android in the future(see second paragraph of this link): http://developer.android.com/reference/android/content/SharedPreferences.html But if you want to read shared preferences of other apps you can follow this link: http://chrisrisner.com/Accessing-the-Shared-Preferences-of-a-Different-Application-in-Android – Nipun Anand Sep 14 '14 at 05:35
0

I agree that Shared Preferences with world_readable will not sufficient for you or sharing across internet is not possible, but still you can do one thing.

Using Broadcast receivers and Custom Broadcasts. with redundant common data across all apps with shared preferences.

Who updates the data will send a broadcast to system. All the apps implement broadcast receiver. when ever a new broadcast received they update the data in shared preferences. App A -> sends broadcast when data is updated.

If App B is installed already App B also receives broadcast and save the data from that intent.

if App B updates new data. APP B -> sends broadcast for the common data Other Apps will update data.

  • Only common data will be lost when all apps are removed. If at least one app is installed data persists.
Gnanendra Kumar
  • 1,096
  • 11
  • 11
0

Preference data will always be stored inside each applications own context. Use sharedUserId and have a new preference file created in all the apps. On opening each app, the app has to check for the preference value from all other applications context and should write into its preference based on last updated time value available in the preference to find the latest updated one.

Whenever any app is opened, the latest data will be stored in its local. if any of the app is installed or uninstalled, this should work fine.

Bala
  • 53
  • 1
  • 6