设置了多个共享preferences实例/文件默认值 [英] Setting default values of multiple SharedPreferences instances/files

查看:143
本文介绍了设置了多个共享preferences实例/文件默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个应用程序,它可以节省preferences到两个文件的 preferences1.xml preferences2.xml 的。然后,我可以检索与以下code引用相应的对象:

Suppose that I have an application which saves preferences onto two files, preferences1.xml and preferences2.xml. Then, I can retrieve references to the corresponding objects with the following code:

SharedPreferences sharedPrefs1 = getSharedPreferences("preferences1", MODE_PRIVATE);
SharedPreferences sharedPrefs2 = getSharedPreferences("preferences2", MODE_PRIVATE);

在这种方式,我可以操控preferences为和注册监听器更改两者。

In this way I can manipulate the preferences for both and register listeners for changes on both.

我有关于这两个文件的初始化有些疑惑,用的 setDefaultValues​​ 的:

I have some doubts about the initialization of those two files, with setDefaultValues:

问题1 - preferenceFragment背景:我创建了一个的 preferenceActivity 的两个 preferenceFragments 的,并在所述的的onCreate 的每一个予执行以下$ C $的c方法(替换1和2片段1和X 2):

Question 1 - PreferenceFragment context: I've created a PreferenceActivity with two PreferenceFragments and within the onCreate method of each one I execute the following code (replace X with 1 and 2 for fragment 1 and 2):

PreferenceManager pm = getPreferenceManager();
pm.setSharedPreferencesName("preferencesX");

PreferenceManager.setDefaultValues(getActivity(),R.xml.preference_fragmentX, false);

我见过的两个片段设置正确的preferences其默认值时启动..但是,鉴于我只能看到一个 _has_set_default_values​​.xml 文件中 shared_ preFS 应用程序的目录,它是如何理解的时候性能的 preferences1.xml preferences2.xml 已经被设定?该文件一旦创建为的 setDefaultValues​​ 的是所谓的第一次打开的<​​em> preferenceFragment 的,但即使在这之后,如果我打开第二的 preferenceFragment 的是正确初始化的默认值。它是如何理解它尚未初始化的 preferences2.xml 的是什么目的 _has_set_default_values​​.xml 因为它不包含信息的事实哪些prefereces文件已被初始化?

I've seen that both fragments correctly set their preferences with their default values when launched.. but, given the fact that I can see only a single _has_set_default_values.xml file in the shared_prefs directory of the app, how does it understand when properties of preferences1.xml and preferences2.xml have been already set? This file is created as soon as setDefaultValues is called in the first opened PreferenceFragment, but even after that, if I open the second PreferenceFragment it correctly initializes default values. How does it understand that it has not yet initialized preferences2.xml and what is the purpose of _has_set_default_values.xml given the fact that it does not contains information about which prefereces files have been initialized?

问题2 - 标准活动方面:当我开始我的应用程序中, preferenceActivity 的是不是第一次活动开始,用户可能永远不会打开它,所以我想初始化也是在主的活动的默认值不变两个preference文件的,我该怎么办呢?对于默认共享preferences很容易:

Question 2 - Standard Activity context: when I start my app, the PreferenceActivity is not the first activity started and the user may never open it, so I'd like to initialize the two preference files with their default values also in the main Activity, how can I do that? For default shared preferences it is easy:

PreferenceManager.setDefaultValues(this, R.xml.default_preferences, false); 

对于双preference文件,我应该怎么办?我不能做类似下面的,因为我不能检索的一个实例的 preferenceManager 的像在 preferenceFragment 的类:

PreferenceManager pm = getPreferenceManager(); // NOT AVAILABLE AND NOT WANTED
pm.setSharedPreferencesName("preferences1");
PreferenceManager.setDefaultValues(getActivity(),R.xml.preference_fragment1, false);

PreferenceManager pm = getPreferenceManager(); // NOT AVAILABLE AND NOT WANTED
pm.setSharedPreferencesName("preferences2");            
PreferenceManager.setDefaultValues(getActivity(),R.xml.preference_fragment2, false);

在这里的主要活动我没有需要改变的preference文件在其上的 preferenceManager 的将工作,因为我们不是在 preferenceActivity 的,我只是要初始化这两个文件...任何帮助吗?我希望我已经发布了一个清晰的问题,即使太长。

Here in the main Activity I do not have the need to change the preference files on which the PreferenceManager will work, since we're not in a PreferenceActivity, I simply want to initialize those two files... any help? I hope I've posted a clear question, even if too long.

推荐答案

您可以创建一个preference是这样的:

you can create a preference like this:

public class MyPreference {

    private static final String APP_SHARED_PREFS1 = "myPrefc1"; 
    private static final String APP_SHARED_PREFS2 = "myPrefc2"; 
    private SharedPreferences   appSharedPrefs1;
    private SharedPreferences   appSharedPrefs2;
    private Editor              prefsEditor1;
    private Editor              prefsEditor2;


    public MyPreference(Context context) {
        this.appSharedPrefs1 = context.getSharedPreferences(APP_SHARED_PREFS1, Activity.MODE_PRIVATE);
        this.appSharedPrefs2 = context.getSharedPreferences(APP_SHARED_PREFS2, Activity.MODE_PRIVATE);
        this.prefsEditor1 = appSharedPrefs1.edit();
        this.prefsEditor2 = appSharedPrefs2.edit();
    }

 public void saveServices(String servicName, boolean isActivated) {
        prefsEditor1.putBoolean(servicName, isActivated);
        prefsEditor1.commit();
        prefsEditor2.putBoolean(servicName, isActivated);
        prefsEditor2.commit();
    }

在你保存服务或别的东西,你想要做你的preference数据将节省文件

on your save service or something else you wanna do with your preference the data will save in both file.

和你的第二个问题:

创建像

create a Global class like G

,然后宣布你的preference是这样的:

and then declare your preference like this:

public class G extends Application{

    public static Activity currentActivity = null;
    public static MyPreference myAppPref = null;


    @Override
    public void onCreate() {

        super.onCreate();

         myAppPref = new MyPreference(G.this);


    }


}

当你的主要活动运行,你应该做的是这样的:

and when your main activity runs you should do like this:

G.currentActivity = MainActivity.this;
G. myAppPref.saveServices("testpref", true);

这篇关于设置了多个共享preferences实例/文件默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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