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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23635644/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 07:19:55  来源:igfitidea点击:

How can I view the shared preferences file using Android Studio?

androidandroid-studiosharedpreferences

提问by JayUser

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 上进行调试。我的手机已经root了。我读到拥有 root 访问权限对于读取这些类型的文件很重要。如果没有办法,那么我将查找如何通过我的程序访问信息然后将其输出到日志cat。不过,希望我可以直接在手机上查看文件,因为它要简单得多。谢谢。

回答by George Dima

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 Device Monitor,转到文件资源管理器,然后浏览“/data/data/<您的包名称>/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 shellas long as your application is the debug version.

如果您使用的是非 root 设备,则无法直接从 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.

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

回答by phnmnn

The Device File Explorer that is part of Android Studio 3.xis really good for exploring your preference file(s), cache items or database.

作为Android Studio 3.x一部分的设备文件资源管理器非常适合探索您的首选项文件、缓存项或数据库。

  1. Shared Preferences /data/data//shared_prefs directory
  1. 共享首选项 /data/data//shared_prefs 目录

enter image description here

在此处输入图片说明

It looks something like this

它看起来像这样

enter image description here

在此处输入图片说明

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.

单击视图 > 工具窗口 > 设备文件资源管理器或单击工具窗口栏中的设备文件资源管理器按钮。

enter image description here

在此处输入图片说明

回答by Maksim Turaev

Stetho

斯特托

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. view and edit sqLite db
  3. view view heirarchy
  4. monitor http network requests
  5. view stream from the device's screen
  6. and more....
  1. 查看和编辑共享首选项
  2. 查看和编辑 sqLite 数据库
  3. 查看查看层次结构
  4. 监控http网络请求
  5. 从设备屏幕查看流
  6. 和更多....

enter image description here

在此处输入图片说明

Basic setup:

基本设置:

  1. in the build.gradle add compile 'com.facebook.stetho:stetho:1.5.0'
  2. in the application's onCreate() add Stetho.initializeWithDefaults(this);
  3. in Chrome on your PC go to the chrome://inspect/
  1. 在 build.gradle 添加 compile 'com.facebook.stetho:stetho:1.5.0'
  2. 在应用程序的 onCreate() 添加 Stetho.initializeWithDefaults(this);
  3. 在 PC 上的 Chrome 中转到chrome://inspect/

UPDATE: Flipper

更新:鳍状肢

Flipperis a newer alternative from facebook. It has more features but for the time writing is only available for Mac, slightly harder to configure and lacks data base debugging, while brining up extreamely enhanced layout inspector

Flipper是 facebook 的更新替代品。它有更多的功能,但目前写作仅适用于 Mac,配置稍难,缺乏数据库调试,同时带来了极其增强的布局检查器

You can also use @Jeffrey suggestion:

您还可以使用@Jeffrey 建议:

  • Open Device File Explorer (Lower Right of screen)
  • Go to data/data/com.yourAppName/shared_prefs
  • 打开设备文件资源管理器(屏幕右下角)
  • 转到 data/data/com.yourAppName/shared_prefs

回答by cYrixmorten

You could simply create a special Activity for debugging purpose:

您可以简单地创建一个特殊的 Activity 用于调试目的:

@SuppressWarnings("unchecked")
public void loadPreferences() {
// create a textview with id (tv_pref) in Layout.
TextView prefTextView;
prefTextView = (TextView) findViewById(R.id.tv_pref);
    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;
        }
        // Every new preference goes to a new line
        prefTextView.append(printVal + "\n\n");     
    }
}
// call loadPreferences() in the onCreate of your Activity.

回答by Niranjan

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

Android Studio -> 设备文件资源管理器(右下角) -> 数据 -> 数据 -> {package.id} -> shared-prefs

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

注意:您需要将移动设备连接到 android studio 并且所选应用程序应处于调试模式

回答by Reaz Murshed

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

这是一篇旧帖子,但我想我应该在这里给出一个图形答案,因为问题是关于查看SharedPreferences.xml使用 Android Studio 的。所以就到这里了。

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

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

enter image description here

在此处输入图片说明

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.

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

enter image description here

在此处输入图片说明

Select the SharedPreferences.xmlfile 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.

我使用了设备模拟器。

回答by Christopher Rucinski

Single or Multiple Shared Preference Files

单个或多个共享首选项文件

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

如果您有多个Shared Preference文件,那么这里是显示所有文件的好方法,但您也可以只传入 1 个文件名。

  • loadSharedPrefs("pref_name");

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

  • loadSharedPrefs("pref_name");

  • 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

Logcat 输出

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

我的Shared Preference值都是String,但这是使用上述两种方法之一的输出...

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﹕ ------------------------------------

回答by user5291072

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

另一种简单的方法是在手机上使用根资源管理器应用程序。

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 rootof your device, not sd card.

然后转到/data/data/package name/shared preferences folder/name of your preferences.xml,您可以使用ES File explorer,然后转到root您设备的 ,而不是sd card

回答by Jeffrey

In Android Studio 3:

Android Studio 3 中

  • Open Device File Explorer (Lower Right of screen).
  • Go to data/data/com.yourAppName/shared_prefs.
  • 打开设备文件资源管理器(屏幕右下角)。
  • 转到 data/data/com.yourAppName/shared_prefs。

or use Android Debug Database

或使用Android 调试数据库

回答by Shwetank

To open shared preferencein android studio

preference在android studio中打开共享

Open device explorer file from right side- data>data> >shared_prefs

从右侧打开设备资源管理器文件-数据>数据>>shared_prefs

find the attached image for more description

找到附加图片以获取更多描述