Android的:如何让所有的偏好XML的名单对我的应用程序来读?(Android: how to

2019-07-18 22:37发布

如何让所有的应用程序首选项列表应用,

1.我保存共享偏好以这种方式

2.我知道,他们是在data/data/app_packagename/shared_prefs


3.问题:可是如何才能让在旋转的所有喜好的xml文件的列表

和阅读各的喜好,我搜索的SO ,但我没有发现这方面,如何做到在读我的应用程序目录中的所有首选项的XML文件,访问首什么帮助?

PS:我知道SharedPreference.getAll(); ,将足以读取一次我得到的文件?

我在位(粗码),试图运行时,它给错误中写道,这里是实现的方法

void getList()
{
  //will be invoked from onCreate to populate spinner,yes spinner is already binded
   PackageManager m = getPackageManager();
        String s = getPackageName();
        try {
            PackageInfo p = m.getPackageInfo(s, 0);
            s = p.applicationInfo.dataDir;
        } catch (NameNotFoundException e) {
            Log.w("yourtag", "Error Package name not found ", e);
        }
        Log.i("dir", s=s+"/shared_prefs");
     //is this write way, how to proceed from here
}

Answer 1:

试试这个

    File prefsdir = new File(getApplicationInfo().dataDir,"shared_prefs");

    if(prefsdir.exists() && prefsdir.isDirectory()){
        String[] list = prefsdir.list();
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, android.R.id.text1,list);
        Spinner sp = (Spinner) findViewById(R.id.spinner1);
        sp.setAdapter(adapter);

    }

//获取所选项目

String item = (String) sp.getSelectedItem();
    //remove .xml from the file name
    String preffile = item.substring(0, item.length()-4);

    SharedPreferences sp2 = getSharedPreferences(preffile, MODE_PRIVATE);
    Map<String, ?> map = sp2.getAll();          

    for (Entry<String, ?> entry : map.entrySet()){
        System.out.println("key is "+ entry.getKey() + " and value is " + entry.getValue());
    }


Answer 2:

如果你想使用反射,有一个@hide功能语境#getSharedPrefsFile(字符串名称)

所以,你会调用

上下文#getSharedPrefsFile(字符串名称).getParentFile()去的shared_prefs DIR参考



Answer 3:

public class Preferences extends PreferenceActivity {

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // load the XML preferences file
        addPreferencesFromResource(R.xml.preferences);
    }
}

然后在你的主类,你可以参考偏好

public class DrinkingBuddy extends Activity 
                           implements OnSharedPreferenceChangeListener {

    private int weight;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);

        // register preference change listener
        prefs.registerOnSharedPreferenceChangeListener(this);

        // and set remembered preferences
        weight = Integer.parseInt((prefs.getString("weightPref", "120");
        // etc
    }

    // handle updates to preferences
    public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
        if (key.equals("weightValues")) {
            weight = Integer.parseInt((prefs.getString("weightPref", "120");
        }
        // etc
    }
}

偏好更新的保存为您处理。

(不太确定公共/私有声明!)。



文章来源: Android: how to get list of all preference xml's for my app and read them?