Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Retrieving shared preferences of other application

I have a settings application from which i have to retrieve other applications preferences, but i don't have the details of keys in them, how can i retrieve all the available keys and values in that preference?

Thanks, Swathi

like image 512
Swathi EP Avatar asked May 17 '11 11:05

Swathi EP


People also ask

Where are shared preferences stored Android?

Android Shared Preferences Overview Android stores Shared Preferences settings as XML file in shared_prefs folder under DATA/data/{application package} directory. The DATA folder can be obtained by calling Environment.

How can I get shared preferences data?

Shared Preferences allow you to save and retrieve data in the form of key,value pair. In order to use shared preferences, you have to call a method getSharedPreferences() that returns a SharedPreference instance pointing to the file that contains the values of preferences.

Can an app have multiple shared pref files?

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


2 Answers

Okay! using this code in Application 1 ( with package name is "com.sharedpref1" ) to store data with Shared Preferences.

SharedPreferences prefs = getSharedPreferences("demopref",                     Context.MODE_WORLD_READABLE);             SharedPreferences.Editor editor = prefs.edit();             editor.putString("demostring", strShareValue);             editor.commit(); 

And using this code in Application 2 to get data from Shared Preferences in Application 1. We can get it because we use MODE_WORLD_READABLE in application 1:

    try {                 con = createPackageContext("com.sharedpref1", 0);                 SharedPreferences pref = con.getSharedPreferences(                         "demopref", Context.MODE_PRIVATE);                 String data = pref.getString("demostring", "No Value");                 displaySharedValue.setText(data);              } catch (NameNotFoundException e) {                 Log.e("Not data shared", e.toString());             } 

More information please visit this URL: http://androiddhamu.blogspot.in/2012/03/share-data-across-application-in.html

like image 171
danhnn.uit Avatar answered Sep 28 '22 10:09

danhnn.uit


Assuming the preference are WORLD_READABLE, this might work:

final ArrayList<HashMap<String,String>> LIST = new ArrayList<HashMap<String,String>>(); // where com.example is the owning  app containing the preferences Context myContext = createPackageContext("com.example", Context.MODE_WORLD_WRITEABLE);  SharedPreferences testPrefs = myContext.getSharedPreferences("test_prefs", Context.MODE_WORLD_READABLE);  Map<String, ?> items = testPrefs .getAll(); for(String s : items.keySet()) {   // do something like String value = items.get(s).toString()); } 
like image 37
jkhouw1 Avatar answered Sep 28 '22 10:09

jkhouw1