Get all SharedPreferences names and all their keys?

I am making a backup program that saved SharedPreferences phone SharedPreferences to a file of my own structure. But I don’t know how to list them all I need:

For example, 2 programs saved their SharedPreferences with the names "Program A" and "Program B" . I need to get a thay String array containing these 2 names. Then I use getSharedPreferences with "Program A", I need to get all the keys that the program saved.

Is it possible?

EDIT1: I DON'T know what programs / actions are on the phone. I want to get ALL keys that every program will be saved. This is exactly the same as you copy all the data on your phone, but only the SharedPreferences values.

For example: your phone has 10 programs, each of which creates SharedPreferences with the name Program 1 to Program 10 (but, of course, any name they want). And I would like to get all these Program 1 to Program 10 String. Then, if Program 1 has 5 keys with the name Key 1 through Key 5 , I want to get these key names.

EDIT2: According to NikolaMKD guide, this is what I have done so far, but the list returns all programs with "No preference" on all lines, even firstly, I saved SharedPreferences with my activity

 public class Test extends ListActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); SharedPreferences preferences = getSharedPreferences("Test", 1); // No matter what name is: Test or the package name Editor editor = preferences.edit(); editor.putInt("TestKey", 0); editor.commit(); List<ResolveInfo> Temp = getPackageManager().queryIntentActivities( new Intent(Intent.ACTION_MAIN, null) .addCategory(Intent.CATEGORY_LAUNCHER), 0); String[] App = new String[Temp.size()]; for (int i = 0; i < Temp.size(); i++) { App[i] = Temp.get(i).activityInfo.name; FileReader reader = null; try { reader = new FileReader("/data/data/" + App[i] + "/shared_prefs/" + App[i] + "_preferences.xml"); } catch (FileNotFoundException e) { reader = null; } if (reader != null) App[i] += " (Have Prefereces)"; else App[i] += " (No Prefereces)"; } setListAdapter(new ArrayAdapter<String>(this, R.layout.main, App)); } } 
+6
source share
5 answers

Get all _preferences.xml from

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

We parse XML, in XML there is a name for each key. I could misunderstand your question.

+10
source

Do not think that it is possible, you can not read other applications / data / data / folder. On the emulator, you can read them because you have root access.

+3
source

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

+3
source

Try the following:

  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()); } 
+2
source

This is a good question, and I don't think there is an easy way to do this. The best way to do this is either to store these names in DefaultSharedPreferences, or some kind of global array, or , you can get sharedPreferences from the activity itself, if you know the identifier of the preference resource:

 String packageName = String packageName = context.getPackageName(); Resources resources = context.getResources(); String sharedPrefName = resources.getResourceEntryName(R.id.PREFERENCES_RESOURCE_ID); sharedPrefName = packageName+"_"+sharedPrefName; 
0
source

Source: https://habr.com/ru/post/892629/


All Articles