View an Android App's shared preferences?

2019-03-14 18:43发布

问题:

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

回答1:

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



回答2:

Running on emulator --> Go to file explorer --> data/data/yourapplication's package/Sharedpreferences.xml can be seen