Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

View an Android App's shared preferences?

When I am working on my app in eclipse, is there a way to see the changes I make to the shared preferences of the app while it is debugging in the emulator? Thanks in advance

like image 727
taormania Avatar asked Jul 06 '12 02:07

taormania


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. getDataDirectory() .

What is a shared preference in Android?

A SharedPreferences object points to a file containing key-value pairs and provides simple methods to read and write them. Each SharedPreferences file is managed by the framework and can be private or shared. This page shows you how to use the SharedPreferences APIs to store and retrieve simple values.

How do we get access to the preference in Android?

The getPreference method uses the getSharedPreferences() method with the name of the activity class for the preference file name. SharedPreferences preferences = getPreferences(MODE_PRIVATE); int storedPreference = preferences. getInt("storedInt", 0);

Can an app have multiple shared pref files?

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


1 Answers

Run project in emulator, then from Eclipse choose menu Windows-> open perspective ->DDMS.
From tab device, choose emulator name, then go to file explorer,expand data->data->yourpackagename, you should see share reference xml file (only work on the emulator or a rooted device). Finally, export this file to windows.
See http://developer.android.com/tools/debugging/ddms.html
Update:
Another way, you can listen shared preference change:

SharedPreferences.OnSharedPreferenceChangeListener prefListener = 
new SharedPreferences.OnSharedPreferenceChangeListener() {
  public void onSharedPreferenceChanged(SharedPreferences prefs,String key) {
if (key.equals("YourKey")) 
     {
          //Get this
     } 
 }

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);          
preferences.registerOnSharedPreferenceChangeListener(prefListener);

See SharedPreferences.onSharedPreferenceChangeListener not being called consistently

like image 101
ductran Avatar answered Nov 04 '22 14:11

ductran