10

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.

user1683087
  • 121
  • 1
  • 1
  • 3

2 Answers2

3

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.

Community
  • 1
  • 1
DeeV
  • 35,865
  • 9
  • 108
  • 95
  • Yes, there are two different applications and each application has it own shared preferences file stored on their own shared_prefs folder. Yes, I know the package name of the other application via the PackageManager, but I have no option to know which activity/class will handle it. I had already seen that link, the problem here is to access to two different shared prefs with the same name - my own and the other application. After opening one, when I try to open the other the reference points to the one initially opened and not the one sent as context. – user1683087 Sep 19 '12 at 15:04
0

you can read the preferences from other app with this snippet.

String PACKAGE_NAME = "com.gr.iasi";
String PREFERENCE_NAME = "GlobalPrefsJorge";


try {
        myContext = createPackageContext(PACKAGE_NAME,Context.MODE_WORLD_WRITEABLE);             
          SharedPreferences testPrefs = myContext.getSharedPreferences(PREFERENCE_NAME, Context.MODE_WORLD_READABLE); 

        Map<String, ?> items = testPrefs .getAll();
        for(String s : items.keySet()){
             //Print keys and values;
            Log.i("***" +s.toString(), items.get(s).toString());
          }

        } catch (NameNotFoundException e) {     
            e.printStackTrace();
        }
Jorgesys
  • 124,308
  • 23
  • 334
  • 268