Get all SharedPreferences names and all their keys?

谁说胖子不能爱 提交于 2019-11-28 12:16:07
Nikola Despotoski

Get all the _preferences.xml from

/data/data/(package)/shared_prefs/(package)_preferences.xml

Parse the XML, in the XML there is name for every key. I might misunderstood your question

Don't think it is possible, You can't read other apps /data/data/ folder. On the emulator you can read them because you have root access.

Use the sharedPreference.getAll(); which returns a Map.

Try this:

    Map<String, ?> allPrefs = prefs.getAll(); //your sharedPreference
    Set<String> set = allPrefs.keySet();
    for(String s : set){
        LOG.d(TAG, s + "<" + allPrefs.get(s).getClass().getSimpleName() +"> =  "
                + allPrefs.get(s).toString());
    }

It's a good question, and I don't think there is a straightforward way of doing it. The best way to do it would be to either store these names in a DefaultSharedPreferences or some sort of global array, or you could retrieve the sharedPreferences from the activity itself if you know the preference resource id:

String packageName = String packageName = context.getPackageName(); 
Resources resources = context.getResources(); 
String sharedPrefName = resources.getResourceEntryName(R.id.PREFERENCES_RESOURCE_ID); 
sharedPrefName = packageName+"_"+sharedPrefName;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!