获取所有 SharedPreferences 名称及​​其所有键?

发布于 2024-11-19 12:41:38 字数 2084 浏览 3 评论 0原文

我正在制作备份程序,将手机 SharedPreferences 数据保存到我自己结构的文件中。但我不知道如何列出我需要的全部内容:

例如,2 个程序使用名称 "Program A""Program B" 保存了它们的 SharedPreferences。我需要获取包含这两个名称的字符串数组。然后,我将 getSharedPreferences 与“程序 A”一起使用,我需要获取该程序已保存的所有密钥。

真的可能吗?

编辑1:我不知道手机上有哪些程序/活动。我想获取每个程序保存的所有密钥。就像您备份所有手机数据一样,但仅备份 SharedPreferences 值。

例如:您的手机有 10 个程序,每个程序都会创建一个名为 Program 1Program 10 的 SharedPreferences(当然,无论他们想要什么名称)。我想获取所有这些Program 1Program 10字符串。然后,如果Program 1有5个按键,分别为Key 1Key 5,我想获取这些按键名称。

编辑2:根据 NikolaMKD 指南,这是我到目前为止所做的,但列表返回所有程序,所有行都带有“无首选项”,即使在第一行,我也保存了 SharedPreferences我的活动:

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));
    }



}

I'm making backup program, that saved the phone SharedPreferences data into a file of my own structure. But I don't know how to list them all, which I need:

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

Is it really possible?

EDIT1: I DON'T know what programs/activities on the phone. I want to get ALL the keys that every programs saved. It just like you back up all your phone data, but only SharedPreferences values.

For example: Your phone has 10 programs, each creates a SharedPreferences named Program 1 to Program 10 (but of course, whatever name they want). And I would like to obtain all these Program 1 to Program 10 String. Then, if Program 1 has 5 keys called Key 1 to Key 5, I want to obtain these keys name.

EDIT2: According to NikolaMKD guide, this is what I've done so far, but the list return all programs, with "No Preferences" at all line, even at the first, I saved the 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));
    }



}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

浅唱ヾ落雨殇 2024-11-26 12:41:38

获取所有 _preferences.xml

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

解析 XML,在 XML 中每个键都有名称。我可能误解了你的问题

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

情话难免假 2024-11-26 12:41:38

试试这个:

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

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());
    }
乖乖哒 2024-11-26 12:41:38

使用 sharedPreference.getAll(); 返回一个 Map

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

雄赳赳气昂昂 2024-11-26 12:41:38

不要认为这是可能的,您无法读取其他应用程序的 /data/data/ 文件夹。在模拟器上,您可以阅读它们,因为您具有 root 访问权限。

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.

简单爱 2024-11-26 12:41:38

这是一个很好的问题,但我认为没有一个简单的方法可以做到这一点。最好的方法是将这些名称存储在 DefaultSharedPreferences 或某种全局数组中,或者如果您知道首选项资源 ID,则可以从活动本身检索共享Preferences:

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

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;
猫瑾少女 2024-11-26 12:41:38

您可以循环访问共享首选项目录中的所有 xml 文件,并获取应用程序中共享首选项名称的所有文件的名称。

File src = new File("/data/data/" + BuildConfig.APPLICATION_ID + "/shared_prefs");
if (src.isDirectory()) {
    String files[] = src.list();
    for (int i = 0; i < files.length; i++) {
        if (files[i].contains(".xml")) {
            reloadSharedPreference(context, files[i].replace(".xml", ""));
        }
    }
}

You can loop through all the xml files in your shared preference directory and get the name of all the files which are the shared preference names in your app.

File src = new File("/data/data/" + BuildConfig.APPLICATION_ID + "/shared_prefs");
if (src.isDirectory()) {
    String files[] = src.list();
    for (int i = 0; i < files.length; i++) {
        if (files[i].contains(".xml")) {
            reloadSharedPreference(context, files[i].replace(".xml", ""));
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文