Android - SharedPreferences via profiles

Is there a way to access SharedPreferences for user profiles on Android?

For example, if UserA changes the application settings, I want this change to also be available to UserB. I tried to use a text file written to getFilesFir() and read it from another account, but when userA writes the file, it is inaccessible to user B. I am writing files with MODE_PRIVATE , since MODE_WORLD_READABLE and MODE_WORLD_WRITABLE are deprecated, but I'm not sure if this helps them.

Does anyone know if there is a way to centralize the settings for my application settings so that it can be accessed by all users? I want nothing on the SD card.

+7
android sharedpreferences user-profile
source share
4 answers

You can not. Only an SD card does not provide user rights.

0
source share

You might want to read the rules of the game when several users are added to the device in this document under the “Effects” section. It should be obvious that what you are trying to accomplish will be a “creative workaround” or simply impossible.

So, what Android tools exist for data exchange applications that are not a file system? Well, the usual mechanism is ContentProvider , but I suspect that two isolated instances of the application cannot access ContentProviders to each other (and, moreover, how would you tell them apart?).

Suppose that user instance applications simply cannot access each other. Maybe they can share some system-level tools that are available to all applications all the time, regardless of who the active user is? The only thing that happens to me when I write this is DropBoxManager . Unfortunately, it is not reliable, since there are restrictions on how much it can store, and these restrictions can cause your data to be pushed out of the queue at any time before it can be read by another instance of the application. In addition, the data there is essentially publicly available, which may not be what you want.

If you want to go crazy, you can go back to ContentProviders and somehow use system-level providers for calendar or contacts for data storage, if both applications know how to find a secret special record. Wild stuff.

Another solution may be to store all your data on a server, and each user enters a key to be able to place and read / write their data block on the server. If not a manual key, try using some “unique” property of the device as a key. This is probably the best solution, since you have more guarantee that something will not accidentally delete or expose your data.

0
source share

Using getSharedPreferences () with MODE_PRIVATE should be enough. The only thing you need is to use the application context. Thus, your general settings will be available from all your actions, regardless of the current user.

 String PREFS_NAME = "com.your_application"; Context context = getApplicationContext(); 

Read an example of preference:

 SharedPreferences reader; reader = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE); reader.getInt("name1", 3); 

Enter an example of preference:

 SharedPreferences.Editor writer; writer = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE).edit(); writer.putString("name2", "string").apply(); 
0
source share

The solution would be to grant user permissions to the content provider and define a protocol for saving and restoring the settings of this content provider.

0
source share

All Articles