Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Shared Preferences across different applications

I've been developing an Android application which interacts with other applications installed on the device via a sharedpreference file.

This file was created as MODE.WORLD_READABLE and every installed application has its own file with a common defined name and property (what changes is the value for that specific property).

For example, AppA and AppB should have each one a shared file named "app_shared_file.xml" with a property "property_shared_file".

From my application I want to first access this file and read that property value, and depending on that result I want to create/update mine.

Nevertheless, I'm having some problems in achieving that. On the Android documentation I see that:

Retrieve and hold the contents of the preferences file 'name', returning a SharedPreferences through which you can retrieve and modify its values. Only one instance of the SharedPreferences object is returned to any callers for the same name, meaning they will see each other's edits as soon as they are made.

After reading the other application shared preferences file, when I try to create/update mine the system instead of editing mine is using the one I have previous read. If on the contrary, I start by updating mine and read the other app later - instead of reading the other one, the one being accessed is mine.

Any thoughts?

The code I'm using for reading the shared pref is [where packageName is the other app pckg]:

Context con = context.createPackageContext(packageName, Context.CONTEXT_IGNORE_SECURITY);
SharedPreferences pref = con.getSharedPreferences(SHARED_PREF_NAME, Context.MODE_WORLD_READABLE);
pref.getBoolean(SHARED_PREF_PROP, false);

The code I'm using for writing in the app shared pref is:

SharedPreferences prefs= getSharedPreferences(SHARED_PREF_NAME, context.MODE_WORLD_READABLE);
SharedPreferences.Editor editor = prefs.edit();
editor.putBoolean(SHARED_PREF_PROP, value); 
editor.commit();

Thanks in advance.

like image 981
user1683087 Avatar asked Sep 19 '12 13:09

user1683087


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 does Android shared preferences do?

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.


1 Answers

SharedPreferences are stored local to the package name of your application. From what it sounds like, you have two completely separate apps with different package names. This means instead of reading from one, you're creating two files of the same name that are stored in different folders. In order for different activities to read from the same SharedPreferences files, you must have them under the same package name as defined by the AndroidManifest file.

The other option, since you know the package name of the other applications (I assume), is to choose the main app that will create it and handle it. Then read directly from the file itself using the direct URI. Since you're setting the permissions to MODE_WORLD_READABLE, the system should allow you to do this. Although, it might block you from accessing the folder itself. I've personally never tried.

I believe I have found the answer you are looking for.

like image 176
DeeV Avatar answered Sep 19 '22 16:09

DeeV