获取所有共享preferences的名字和他们所有的钥匙? [英] Get all SharedPreferences names and all their keys?

查看:163
本文介绍了获取所有共享preferences的名字和他们所有的钥匙?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做的备份方案,即节省了电话共享preferences 数据到我自己的结构的文件。但我不知道该如何一一列举,我需要:

例如,2程序救了他们的共享preferences与名称计划A计划B。我需要获得大公String数组包含这两个名称。然后,我用 getShared preferences 与计划A,我需要得到该程序已保存了所有键。

难道真的有可能吗?

EDIT1:我不知道什么是手机上的程序/活动。我想所有的拯救每一个程序。这就像你备份你所有的手机数据,但只有共享preferences值。

例如:您的手机有10个项目,每到创建一个名为程序1 共享preferences计划10 (但当然,任何名字,他们想要的)。我想获得所有这些程序1 计划10 字符串。然后,如果程序1 拥有5键名为键1 键5 ,我想获得这些密钥的名字。

EDIT2:根据 NikolaMKD 指南,这是我一直在做的一切,到目前为止,但列表返回所有方案,没有preferences行,甚至在第一个,我救的共享preferences我的活动:

 公共类测试扩展ListActivity {

    @覆盖
    保护无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        共享preferences preferences = getShared preferences(测试,1);
        //不管是什么名字是:测试或包的名称
        编辑EDITOR = preferences.edit();
        editor.putInt(密押,0);
        editor.commit();

        名单< ResolveInfo>温度=
            getPackageManager()。queryIntentActivities(
                    新的意图(Intent.ACTION_MAIN,空)
                    .addCategory(Intent.CATEGORY_LAUNCHER),0);
        的String []应用程序=新的String [Temp.size()];
        的for(int i = 0; I< Temp.size();我++){
            应用[I] = Temp.get(我).activityInfo.name;
            FileReader对象读卡器= NULL;
            尝试 {
                读者=新的FileReader(/数据/数据​​/
                        +应用程序[我] +/ shared_ preFS /+应用程序[我] +_ preferences.xml);
            }赶上(FileNotFoundException异常E){
                读者=无效;
            }
            如果(读者!= NULL)
                应用[I] + =(有prefereces);
            其他
                应用[I] + =(没有prefereces);
        }
        setListAdapter(新ArrayAdapter<字符串>(这一点,R.layout.main,APP));
    }



}
 

解决方案

获取所有_ preferences.xml从

/数据/数据​​/(包)/ shared_ preFS /(包)_ preferences.xml

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

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



}

解决方案

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

这篇关于获取所有共享preferences的名字和他们所有的钥匙?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆