Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shared preferences between two processes of the same application

Tags:

I'm writing an Android app that contains both UI and separate processes running. I want to share simple information between the main process and a service defined in my application that is executed in a different process. I find to messy to use AIDL for inter-process communication for this purpose.

The question is: Is it safe to use Shared Preferences of the application for communicating between this two processes? This is: both read and write the same shared preferences.

I'm wondering if it actually works. In android developers reference about shared preferences (http://developer.android.com/reference/android/content/SharedPreferences.html) they state: Note: currently this class does not support use across multiple processes. This will be added later. but I don't know exactly what does this mean.

Thanks for your help

like image 262
Javierfdr Avatar asked Aug 25 '12 19:08

Javierfdr


People also ask

Can an app have multiple shared pref files?

Yes you can maintain as many shared preference files for an app as you can.

What is shared preferences explain with example?

Shared Preferences is the way in which one can store and retrieve small amounts of primitive data as key/value pairs to a file on the device storage such as String, int, float, Boolean that make up your preferences in an XML file inside the app on the device storage.

What is difference between preferences and SharedPreferences?

Preferences: The user interfaces part of the settings. It contains different classes which allow one to compose Settings screens from code or XML. Shared Preferences: These are used to store values in XML files. These files are created, maintained and deleted by Android for you.

What is the difference between internal storage and shared preferences?

Shared Preferences. Store private primitive data in key-value pairs. Internal Storage. Store private data on the device memory.


2 Answers

In Android < 2.3 it works. One process can write changes and the other process can read the changes. The code to read/write shared preferences files (they are actually stored in files) checks if there have been any changes made to the file before reading/writing and they update their cached version accordingly.

In Android > 2.3 it works, but you need to specifically set MODE_MULTI_PROCESS when calling getSharedPreferences().

In Android 2.3 it is broken and it doesn't work :-(

Please note that MODE_MULTI_PROCESS is deprecated in API Level 23 (Android M).

like image 75
David Wasser Avatar answered Sep 29 '22 12:09

David Wasser


You can check out https://github.com/hamsterksu/MultiprocessPreferences library which provides SharedPreferences-like APIs for accessing SharedPreferences data via a ContentProvider. It also looks like a good alternate after Google removed MODE_MULTI_PROCESS from Android 6.

like image 38
Sam Lu Avatar answered Sep 29 '22 13:09

Sam Lu