Sharing and persisting data between multiple Android applications
Asked Answered
Y

4

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.

Younglove answered 12/9, 2014 at 16:3 Comment(3)
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.Paradise
Were you able to solve this problem?Senarmontite
Did you find a solution to this problem ?Historied
F
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/

Faunus answered 12/9, 2014 at 16:24 Comment(2)
But what if the user doesn't have Play Services installed? This approach fails in that case.Younglove
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.Faunus
I
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", "");
Idiomorphic answered 12/9, 2014 at 16:25 Comment(8)
And where would this file reside?Paradise
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.Idiomorphic
Is it safe to save sensitive data using MODE_MULTI_PROCESS?Younglove
So then this will not help the OP as the other apps cannot reach it.Paradise
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.Idiomorphic
shared preferences files are stored in the data folder. Please follow the link below for more details: developer.android.com/guide/topics/data/data-storage.html#prefIdiomorphic
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 desiredFaunus
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): developer.android.com/reference/android/content/… But if you want to read shared preferences of other apps you can follow this link: chrisrisner.com/…Idiomorphic
I
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.
Institutional answered 8/9, 2016 at 12:4 Comment(0)
A
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.

Agony answered 1/3, 2017 at 12:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.