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

前端 未结 14 1069
甜味超标
甜味超标 2020-11-27 11:33

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

14条回答
  •  星月不相逢
    2020-11-27 12:21

    You could simply create a special Activity for debugging purpose:

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

提交回复
热议问题