如何使用Android Studio查看共享首选项文件?

时间:2021-03-02 07:36:15

I'm using shared preferences to store certain values for my app. I would like to see the file where the info is actually stored on my phone. I found many ways to do this on Eclipse, but I'm debugging on Android Studio. My phone is rooted. I read that having root access is important to read these types of files. If there is no way, then I will look up how to access the info through my program then output it to log cat. Hopefully, though, I can just view the file on the phone directly as it is much simpler. Thanks.

我正在使用共享首选项来为我的应用存储某些值。我在Eclipse上找到了很多方法,但是我正在Android Studio上调试。我的电话是根深蒂固的。我读到,有根访问权对于读取这些类型的文件很重要。如果没有办法,那么我将查找如何通过我的程序访问信息然后输出到log cat。不过,我希望我可以直接在电话上查看这个文件,因为它要简单得多。谢谢。

9 个解决方案

#1


60  

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.

从Android Studio开始,启动Android设备监视器,进入File Explorer,浏览“/data/data/< package >/shared_prefs/”。您将在那里找到XML…你也可以复制一份供检查。

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.

如果你有一个非根设备,不可能直接从Android Studio进行。但是,只要应用程序是调试版本,就可以使用adb shell访问文件。

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.

之后,您可以从/sdcard目录中使用adb提取文件。

#2


36  

You can use http://facebook.github.io/stetho/ for accessing your shared preferences while your application is in the debug mode. No Root

您可以使用http://facebook.github。io/stetho/用于在应用程序处于调试模式时访问共享首选项。没有根

features:

特点:

  1. view and edit sharedpreferences
  2. 查看和编辑sharedpreferences
  3. view and edit sqLite db
  4. 查看和编辑sqLite db
  5. view view heirarchy
  6. 视图查看等级秩序
  7. monitor http network requests
  8. 监控http网络请求
  9. view stream from the device's screen
  10. 从设备的屏幕上查看流
  11. and more....
  12. 和更多....

如何使用Android Studio查看共享首选项文件?

Basic setup:

基本设置:

  1. in the build.gradle add compile 'com.facebook.stetho:stetho:1.5.0'
  2. 在构建。gradle添加编译com.facebook.stetho:stetho:1.5.0的
  3. in the application's onCreate() add Stetho.initializeWithDefaults(this);
  4. 在应用程序的onCreate()中添加Stetho.initializeWithDefaults(this);
  5. in Chrome on your PC go to the chrome://inspect/
  6. 在你的电脑上的Chrome中去Chrome://检查/

#3


13  

You could simply create a special Activity for debugging purpose:

您可以为调试目的创建一个特殊的活动:

@SuppressWarnings("unchecked")
public void loadPreferences() {
    Map<String, ?> prefs = PreferenceManager.getDefaultSharedPreferences(
            context).getAll();
    for (String key : prefs.keySet()) {
        Object pref = prefs.get(key);
        String printVal = "";
        if (pref instanceof Boolean) {
            printVal =  key + " : " + (Boolean) pref;
        }
        if (pref instanceof Float) {
            printVal =  key + " : " + (Float) pref;
        }
        if (pref instanceof Integer) {
            printVal =  key + " : " + (Integer) pref;
        }
        if (pref instanceof Long) {
            printVal =  key + " : " + (Long) pref;
        }
        if (pref instanceof String) {
            printVal =  key + " : " + (String) pref;
        }
        if (pref instanceof Set<?>) {
            printVal =  key + " : " + (Set<String>) pref;
        }

        // create a TextView with printVal as text and add to layout
    }
}

#4


5  

Another simple way would be using a root explorer app on your phone.

另一种简单的方法是在你的手机上使用一个root explorer应用程序。

Then go to /data/data/package name/shared preferences folder/name of your preferences.xml, you can use ES File explorer, and go to the root of your device, not sd card.

然后转到/data/data/package name/shared preferences文件夹/name of your preferences。xml,您可以使用ES文件资源管理器,并访问设备的根目录,而不是sd卡。

#5


5  

This is an old post, but I though I should put a graphical answer here as the question is about viewing the SharedPreferences.xml using Android Studio. So here it goes.

这是一个旧的帖子,但是我想我应该在这里放一个图形化的答案,因为问题是关于查看SharedPreferences的。xml使用Android工作室。这里。

Go to the Tools -> Android Device Monitor. Open the device monitor by clicking it.

到工具-> Android设备监视器。通过单击打开设备监视器。

如何使用Android Studio查看共享首选项文件?

Then you need to select the File Explorer tab in the device monitor. Find the data folder and find another data folder inside it. It will contain a folder having the name of your application package and there will be the desired SharedPreferences.xml.

然后需要在设备监视器中选择File Explorer选项卡。找到数据文件夹并在其中找到另一个数据文件夹。它将包含一个具有应用程序包名称的文件夹,其中将包含所需的sharedpreferenceses.xml。

如何使用Android Studio查看共享首选项文件?

Select the SharedPreferences.xml file and then pull and save the file in your computer using the button marked at the top-right corner of the image above.

选择SharedPreferences。xml文件,然后使用上图右上角标记的按钮将文件拖放到计算机中。

I've used a device emulator.

我使用了一个设备模拟器。

#6


3  

Single or Multiple Shared Preference Files

If you have multiple Shared Preference files, then here is a good way to show all of them, but you can just pass in 1 filename, too.

如果您有多个共享的首选项文件,那么这里有一个很好的方法来显示它们,但是您也可以传入一个文件名。

  • loadSharedPrefs("pref_name");

    loadSharedPrefs(“pref_name”);

  • loadSharedPrefs("shared_pref1", "shared_pref2", "shared_pref3");

    loadSharedPrefs(“shared_pref1”、“shared_pref2”、“shared_pref3”);

Choose one of the following to suit your needs...

选择以下其中一个来满足你的需要……

Single-Type Values

public void loadSharedPrefs(String ... prefs) {

    // Logging messages left in to view Shared Preferences. I filter out all logs except for ERROR; hence why I am printing error messages.

    Log.i("Loading Shared Prefs", "-----------------------------------");
    Log.i("----------------", "---------------------------------------");

    for (String pref_name: prefs) {

        SharedPreferences preference = getSharedPreferences(pref_name, MODE_PRIVATE);
        for (String key : preference.getAll().keySet()) {

            Log.i(String.format("Shared Preference : %s - %s", pref_name, key),
                  preference.getString(key, "error!"));

        }

        Log.i("----------------", "---------------------------------------");

    }

    Log.i("Finished Shared Prefs", "----------------------------------");

}

Multiple-Type Values

public void loadSharedPrefs(String ... prefs) {

    // Define default return values. These should not display, but are needed
    final String STRING_ERROR = "error!";
    final Integer INT_ERROR = -1;
    // ...
    final Set<String> SET_ERROR = new HashSet<>(1);

    // Add an item to the set
    SET_ERROR.add("Set Error!");

    // Loop through the Shared Prefs
    Log.i("Loading Shared Prefs", "-----------------------------------");
    Log.i("------------------", "-------------------------------------");

    for (String pref_name: prefs) {

        SharedPreferences preference = getSharedPreferences(pref_name, MODE_PRIVATE);
        Map<String, ?> prefMap = preference.getAll();

        Object prefObj;
        Object prefValue = null;

        for (String key : prefMap.keySet()) {

            prefObj = prefMap.get(key);

            if (prefObj instanceof String) prefValue = preference.getString(key, STRING_ERROR);
            if (prefObj instanceof Integer) prefValue = preference.getInt(key, INT_ERROR);
            // ...
            if (prefObj instanceof Set) prefValue = preference.getStringSet(key, SET_ERROR);

            Log.i(String.format("Shared Preference : %s - %s", pref_name, key),
                  String.valueOf(prefValue));

        }

        Log.i("------------------", "-------------------------------------");

    }

    Log.i("Loaded Shared Prefs", "------------------------------------");

}

}

Logcat Output

My Shared Preference values are all String, but this is the output using either of the 2 methods above...

我的共享首选项值都是字符串,但这是使用上述两种方法中的任何一种的输出……

I/Loading Shared Prefs﹕ -----------------------------------
I/------------------﹕ -------------------------------------
I/Shared Preference : FAVORITE - 135397﹕ Jurassic World
I/Shared Preference : FAVORITE - 87101﹕ Terminator Genisys
I/Shared Preference : FAVORITE - 177677﹕ Mission: Impossible – Rogue Nation
I/------------------﹕ -------------------------------------
I/Shared Preference : WATCHED - 177677﹕ Mission: Impossible – Rogue Nation
I/Shared Preference : WATCHED - 157336﹕ Interstellar
I/Shared Preference : WATCHED - 135397﹕ Jurassic World
I/Shared Preference : WATCHED - 87101﹕ Terminator Genisys
I/------------------﹕ -------------------------------------
I/Shared Preference : WILL_WATCH - 211672﹕ Minions
I/Shared Preference : WILL_WATCH - 102899﹕ Ant-Man
I/------------------﹕ -------------------------------------
I/Loaded Shared Prefs﹕ ------------------------------------

#7


2  

Run the application in Emulator after you insert some data, just close the application.

在插入一些数据后,在模拟器中运行应用程序,只需关闭应用程序。

Now open the DDMS or Android Monitor and select your emulator, on the right side you can see the File Explorer, look for Data folder in it and look for your application package that you have created, in that you can find the shared preference file open it , you can see the XML file, click it and click the pull a file from the device button in the top right corner.

现在打开DDMS或Android监视和选择你的模拟器,可以看到右边的文件浏览器,寻找数据文件夹中,寻找应用程序包,您已经创建了,在那你可以找到共同的偏好文件打开它,你可以看到XML文件,单击它并单击将一个文件从设备按钮在右上角。

The XML file will be saved in your desired location, then you can open it using any editor like notepad++ and can view the data you have entered.

XML文件将保存在您想要的位置,然后您可以使用像notepad++这样的编辑器打开它,并且可以查看您输入的数据。

#8


1  

I always find these commands useful in console:

我总是觉得这些命令在控制台很有用:

  1. Find the correct file name

    找到正确的文件名

    adb shell

    亚行壳

    ls /data/data/com.your.package/shared_prefs

    ls /数据/数据/ com.your.package / shared_prefs

  2. Get the file to local directory

    将文件获取到本地目录。

    adb pull /data/data/com.your.package/shared_prefs/the_file_you_want $local_dir

    亚行拉/数据/数据/ com.your。包/ shared_prefs the_file_you_want local_dir美元

  3. Check it in your $local_dir.

    在$local_dir中检查它。

#9


1  

Android Studio -> Device File Explorer (right bottom corner) -> data -> data -> {package.id} -> shared-prefs

Android Studio ->设备文件浏览器(右下角)->数据->数据->{包。id } - > shared-prefs

Note: You need to connect mobile device to android studio and selected application should be in debug mode

注意:您需要将移动设备连接到android studio,选择的应用程序应该处于调试模式

#1


60  

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.

从Android Studio开始,启动Android设备监视器,进入File Explorer,浏览“/data/data/< package >/shared_prefs/”。您将在那里找到XML…你也可以复制一份供检查。

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.

如果你有一个非根设备,不可能直接从Android Studio进行。但是,只要应用程序是调试版本,就可以使用adb shell访问文件。

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.

之后,您可以从/sdcard目录中使用adb提取文件。

#2


36  

You can use http://facebook.github.io/stetho/ for accessing your shared preferences while your application is in the debug mode. No Root

您可以使用http://facebook.github。io/stetho/用于在应用程序处于调试模式时访问共享首选项。没有根

features:

特点:

  1. view and edit sharedpreferences
  2. 查看和编辑sharedpreferences
  3. view and edit sqLite db
  4. 查看和编辑sqLite db
  5. view view heirarchy
  6. 视图查看等级秩序
  7. monitor http network requests
  8. 监控http网络请求
  9. view stream from the device's screen
  10. 从设备的屏幕上查看流
  11. and more....
  12. 和更多....

如何使用Android Studio查看共享首选项文件?

Basic setup:

基本设置:

  1. in the build.gradle add compile 'com.facebook.stetho:stetho:1.5.0'
  2. 在构建。gradle添加编译com.facebook.stetho:stetho:1.5.0的
  3. in the application's onCreate() add Stetho.initializeWithDefaults(this);
  4. 在应用程序的onCreate()中添加Stetho.initializeWithDefaults(this);
  5. in Chrome on your PC go to the chrome://inspect/
  6. 在你的电脑上的Chrome中去Chrome://检查/

#3


13  

You could simply create a special Activity for debugging purpose:

您可以为调试目的创建一个特殊的活动:

@SuppressWarnings("unchecked")
public void loadPreferences() {
    Map<String, ?> prefs = PreferenceManager.getDefaultSharedPreferences(
            context).getAll();
    for (String key : prefs.keySet()) {
        Object pref = prefs.get(key);
        String printVal = "";
        if (pref instanceof Boolean) {
            printVal =  key + " : " + (Boolean) pref;
        }
        if (pref instanceof Float) {
            printVal =  key + " : " + (Float) pref;
        }
        if (pref instanceof Integer) {
            printVal =  key + " : " + (Integer) pref;
        }
        if (pref instanceof Long) {
            printVal =  key + " : " + (Long) pref;
        }
        if (pref instanceof String) {
            printVal =  key + " : " + (String) pref;
        }
        if (pref instanceof Set<?>) {
            printVal =  key + " : " + (Set<String>) pref;
        }

        // create a TextView with printVal as text and add to layout
    }
}

#4


5  

Another simple way would be using a root explorer app on your phone.

另一种简单的方法是在你的手机上使用一个root explorer应用程序。

Then go to /data/data/package name/shared preferences folder/name of your preferences.xml, you can use ES File explorer, and go to the root of your device, not sd card.

然后转到/data/data/package name/shared preferences文件夹/name of your preferences。xml,您可以使用ES文件资源管理器,并访问设备的根目录,而不是sd卡。

#5


5  

This is an old post, but I though I should put a graphical answer here as the question is about viewing the SharedPreferences.xml using Android Studio. So here it goes.

这是一个旧的帖子,但是我想我应该在这里放一个图形化的答案,因为问题是关于查看SharedPreferences的。xml使用Android工作室。这里。

Go to the Tools -> Android Device Monitor. Open the device monitor by clicking it.

到工具-> Android设备监视器。通过单击打开设备监视器。

如何使用Android Studio查看共享首选项文件?

Then you need to select the File Explorer tab in the device monitor. Find the data folder and find another data folder inside it. It will contain a folder having the name of your application package and there will be the desired SharedPreferences.xml.

然后需要在设备监视器中选择File Explorer选项卡。找到数据文件夹并在其中找到另一个数据文件夹。它将包含一个具有应用程序包名称的文件夹,其中将包含所需的sharedpreferenceses.xml。

如何使用Android Studio查看共享首选项文件?

Select the SharedPreferences.xml file and then pull and save the file in your computer using the button marked at the top-right corner of the image above.

选择SharedPreferences。xml文件,然后使用上图右上角标记的按钮将文件拖放到计算机中。

I've used a device emulator.

我使用了一个设备模拟器。

#6


3  

Single or Multiple Shared Preference Files

If you have multiple Shared Preference files, then here is a good way to show all of them, but you can just pass in 1 filename, too.

如果您有多个共享的首选项文件,那么这里有一个很好的方法来显示它们,但是您也可以传入一个文件名。

  • loadSharedPrefs("pref_name");

    loadSharedPrefs(“pref_name”);

  • loadSharedPrefs("shared_pref1", "shared_pref2", "shared_pref3");

    loadSharedPrefs(“shared_pref1”、“shared_pref2”、“shared_pref3”);

Choose one of the following to suit your needs...

选择以下其中一个来满足你的需要……

Single-Type Values

public void loadSharedPrefs(String ... prefs) {

    // Logging messages left in to view Shared Preferences. I filter out all logs except for ERROR; hence why I am printing error messages.

    Log.i("Loading Shared Prefs", "-----------------------------------");
    Log.i("----------------", "---------------------------------------");

    for (String pref_name: prefs) {

        SharedPreferences preference = getSharedPreferences(pref_name, MODE_PRIVATE);
        for (String key : preference.getAll().keySet()) {

            Log.i(String.format("Shared Preference : %s - %s", pref_name, key),
                  preference.getString(key, "error!"));

        }

        Log.i("----------------", "---------------------------------------");

    }

    Log.i("Finished Shared Prefs", "----------------------------------");

}

Multiple-Type Values

public void loadSharedPrefs(String ... prefs) {

    // Define default return values. These should not display, but are needed
    final String STRING_ERROR = "error!";
    final Integer INT_ERROR = -1;
    // ...
    final Set<String> SET_ERROR = new HashSet<>(1);

    // Add an item to the set
    SET_ERROR.add("Set Error!");

    // Loop through the Shared Prefs
    Log.i("Loading Shared Prefs", "-----------------------------------");
    Log.i("------------------", "-------------------------------------");

    for (String pref_name: prefs) {

        SharedPreferences preference = getSharedPreferences(pref_name, MODE_PRIVATE);
        Map<String, ?> prefMap = preference.getAll();

        Object prefObj;
        Object prefValue = null;

        for (String key : prefMap.keySet()) {

            prefObj = prefMap.get(key);

            if (prefObj instanceof String) prefValue = preference.getString(key, STRING_ERROR);
            if (prefObj instanceof Integer) prefValue = preference.getInt(key, INT_ERROR);
            // ...
            if (prefObj instanceof Set) prefValue = preference.getStringSet(key, SET_ERROR);

            Log.i(String.format("Shared Preference : %s - %s", pref_name, key),
                  String.valueOf(prefValue));

        }

        Log.i("------------------", "-------------------------------------");

    }

    Log.i("Loaded Shared Prefs", "------------------------------------");

}

}

Logcat Output

My Shared Preference values are all String, but this is the output using either of the 2 methods above...

我的共享首选项值都是字符串,但这是使用上述两种方法中的任何一种的输出……

I/Loading Shared Prefs﹕ -----------------------------------
I/------------------﹕ -------------------------------------
I/Shared Preference : FAVORITE - 135397﹕ Jurassic World
I/Shared Preference : FAVORITE - 87101﹕ Terminator Genisys
I/Shared Preference : FAVORITE - 177677﹕ Mission: Impossible – Rogue Nation
I/------------------﹕ -------------------------------------
I/Shared Preference : WATCHED - 177677﹕ Mission: Impossible – Rogue Nation
I/Shared Preference : WATCHED - 157336﹕ Interstellar
I/Shared Preference : WATCHED - 135397﹕ Jurassic World
I/Shared Preference : WATCHED - 87101﹕ Terminator Genisys
I/------------------﹕ -------------------------------------
I/Shared Preference : WILL_WATCH - 211672﹕ Minions
I/Shared Preference : WILL_WATCH - 102899﹕ Ant-Man
I/------------------﹕ -------------------------------------
I/Loaded Shared Prefs﹕ ------------------------------------

#7


2  

Run the application in Emulator after you insert some data, just close the application.

在插入一些数据后,在模拟器中运行应用程序,只需关闭应用程序。

Now open the DDMS or Android Monitor and select your emulator, on the right side you can see the File Explorer, look for Data folder in it and look for your application package that you have created, in that you can find the shared preference file open it , you can see the XML file, click it and click the pull a file from the device button in the top right corner.

现在打开DDMS或Android监视和选择你的模拟器,可以看到右边的文件浏览器,寻找数据文件夹中,寻找应用程序包,您已经创建了,在那你可以找到共同的偏好文件打开它,你可以看到XML文件,单击它并单击将一个文件从设备按钮在右上角。

The XML file will be saved in your desired location, then you can open it using any editor like notepad++ and can view the data you have entered.

XML文件将保存在您想要的位置,然后您可以使用像notepad++这样的编辑器打开它,并且可以查看您输入的数据。

#8


1  

I always find these commands useful in console:

我总是觉得这些命令在控制台很有用:

  1. Find the correct file name

    找到正确的文件名

    adb shell

    亚行壳

    ls /data/data/com.your.package/shared_prefs

    ls /数据/数据/ com.your.package / shared_prefs

  2. Get the file to local directory

    将文件获取到本地目录。

    adb pull /data/data/com.your.package/shared_prefs/the_file_you_want $local_dir

    亚行拉/数据/数据/ com.your。包/ shared_prefs the_file_you_want local_dir美元

  3. Check it in your $local_dir.

    在$local_dir中检查它。

#9


1  

Android Studio -> Device File Explorer (right bottom corner) -> data -> data -> {package.id} -> shared-prefs

Android Studio ->设备文件浏览器(右下角)->数据->数据->{包。id } - > shared-prefs

Note: You need to connect mobile device to android studio and selected application should be in debug mode

注意:您需要将移动设备连接到android studio,选择的应用程序应该处于调试模式