Home » Android » sqlite – Sharing and persisting data between multiple Android applications

sqlite – Sharing and persisting data between multiple Android applications

Posted by: admin May 14, 2020 Leave a comment

Questions:

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.

How to&Answers:

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/

Answer:

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", "");

Answer:

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.

Answer:

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.