1

I am using getDefaultSharedPreferences(con) in my application to store preferences. Now I want to access this shared preference in another application. I used following method:

con = this.createPackageContext("com.example.preferences", Context.CONTEXT_INCLUDE_CODE | Context.CONTEXT_IGNORE_SECURITY);
SharedPreferences filePref = PreferenceManager.getDefaultSharedPreferences(con);
            if(filePref != null){
                Toast.makeText(getApplicationContext(), "file pref not null --- ", Toast.LENGTH_LONG).show();   
            }else{
                Toast.makeText(getApplicationContext(), "file pref is null **** ", Toast.LENGTH_LONG).show();
            }
            Map<String,?> allkeys = filePref.getAll();
            Toast.makeText(getApplicationContext(), "file pref size **** "+allkeys.size(), Toast.LENGTH_LONG).show();
            for(Map.Entry<String,?> entry : allkeys.entrySet()){
                Toast.makeText(getApplicationContext(), "~~~ file pref --- map values --- "+entry.getKey() + ": "+entry.getValue().toString(), Toast.LENGTH_LONG).show();
            }

Another way which tried is specifying file name & accessing it, as follow;

SharedPreferences filePref = gvcon.getSharedPreferences("com.example.preferences_preferences", Context.MODE_PRIVATE);

With this method I am able to access SharedPrefernce file, it returns file is not null but when I check for file size it shows 0. I am not able to read preference value from file. I used shareduserid of another application so that I get full access to that application. What is the proper way to go about this?

TronicZomB
  • 8,667
  • 7
  • 35
  • 50
Namrata
  • 31
  • 2
  • take a look at this reply http://stackoverflow.com/a/13236870/1936366 and this http://stackoverflow.com/questions/6030321/android-retrieving-shared-preferences-of-other-application – Onur A. Jun 26 '13 at 14:47

1 Answers1

0

I'm not sure that I understood your problem completely, but some times this implementation will help you to access data across packages. Use getSharedPreferences() to store data in shared preferences

This method will store data in shared preferences

public void dataWriter(){
    String strShareValue = "Hello! this is shared data";
    SharedPreferences prefs = getSharedPreferences("demopref",Context.MODE_WORLD_READABLE);
    SharedPreferences.Editor editor = prefs.edit();
    editor.putString("demostring", strShareValue);
    editor.commit();
}

You can access that shared data from another package using this code sample

public void dataRead(){
 Context con;
     try {
         con = createPackageContext("PACKAGE NAME THAT SHARES DATA", 0);
         SharedPreferences pref = con.getSharedPreferences("demopref", Context.MODE_PRIVATE);
         String dataShared = pref.getString("demostring", "No Value");
      }
     catch (NameNotFoundException e) {
         Log.e("Not data shared", e.toString());
      }
    }
}
  • 1
    Thanks for your reply. My concern is when sharedpreferences are created without specifying name but using default method, ie using prefernceactivity & are stored under /data/data/packagename_preference.xml In this case i am not able to read preference value from xml file. – Namrata Jun 26 '13 at 16:56