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() .
To retrieve an instance of SharedPreferences that the preference hierarchy in this activity will use, call getDefaultSharedPreferences(android. content. Context) with a context in the same package as this activity.
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.
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.
The Device File Explorer that is part of Android Studio 3.x is really good for exploring your preference file(s), cache items or database.
It looks something like this
To open The Device File Explorer:
Click View > Tool Windows > Device File Explorer or click the Device File Explorer button in the tool window bar.
From Android Studio , start Android Device Monitor, go to File Explorer, and browse "/data/data/< name of your package >/shared_prefs/". You will find the XML there... and also you can copy it for inspection.
If you have a non-rooted device it's not possible to do that directly from Android Studio. However, you can access the file with adb shell
as long as your application is the debug version.
adb shell
run-as your.app.id
chmod 777 shared_prefs/your.app.id_preferences.xml
exit # return to default user
cp /data/data/your.app.id/shared_prefs/your.app.id_preferences.xml /sdcard
After that you can pull the file from /sdcard directory with adb.
Flipper is a newer alternative from facebook. It has more features. And actively maintained
You can use http://facebook.github.io/stetho/ for accessing your shared preferences while your application is in the debug mode. No Root
features:
Basic setup:
compile 'com.facebook.stetho:stetho:1.5.0'
Stetho.initializeWithDefaults(this);
- Open Device File Explorer (Lower Right of screen)
- Go to
data/data/com.yourAppName/shared_prefs
Android Studio -> Device File Explorer (right bottom corner) -> data -> data -> {package.id} -> shared-prefs
Note: You need to connect mobile device to android studio and selected application should be in debug mode
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With