在使用时getDefaultShared preferences和getShared preferences [英] when use getDefaultSharedPreferences and getSharedPreferences

查看:458
本文介绍了在使用时getDefaultShared preferences和getShared preferences的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是好奇。有3种方式:

I just curious. There are 3 method:

1. getPreferenceManager().setSharedPreferencesName(String PrefName);

2. PreferenceManager.getDefaultSharedPreferences(Context context)

3. Context.getSharedPreferences (String name, int mode)

据我所知,第三种方法只用于当使用第一种方法,对吗? 但随着3的方法,我们还可以使用添加preferencesFromResource(INT渣油); 那么,有什么区别?我们什么时候可以使用这些方法中的一个? 谢谢!

As I know, the third method is only used when the first method is used, right? But with 3 method we also use addPreferencesFromResource(int resID); so, what is the difference? When can we use one of these method? Thanks!

推荐答案

让我们一步步时间:

  1. setShared preferencesName()的方法,允许设置preference组供以后使用的名称。使用辅助类的时,这是例如有益 preferencesActivity 的调用加载从XML资源文件preferences前添加preferencesFromResource()。因此,它并不像普通如你上面提到的其他两种方法。

  1. setSharedPreferencesName() is method that allows to set the name of the preference group for later use. This is helpful for example when using the helper class of PreferencesActivity before loading a preferences from XML resource file by calling addPreferencesFromResource(). It is therefore not as common as the other 2 methods you mentioned above.

getDefaultShared preferences()使用缺省名称,通常存储为/data/data/com.package.name/shared_$p$pfs /com.package.name_$p$pferences.xml。 它常用。请注意,此默认设置的每个应用程序的。

getDefaultSharedPreferences() uses a default name, usually stored as /data/data/com.package.name/shared_prefs/com.package.name_preferences.xml. It is commonly used. Note that this default is set per application.

另一种方法 - getShared preferences()需要表明一种特殊的preference(文件)的名称和操作模式。

The alternative method - getSharedPreferences() requires to indicate a specific preference (file) name and an operation mode.

随着也出现在有关共享preferences 的另外一个答案, getDefaultShared preferences()实际上使用Context.getShared preferences,所以结果是一样的,但没有flexbility拆分到多个preference文件,由 getShared preferences提供()。使用共享应用程序之间的preferences 一个 MODE_WORLD_READABLE 运行指标也是使用 getShared preferences(),但很少使用的东西可能。

As appears also in another answer about shared preferences, getDefaultSharedPreferences() in fact uses Context.getSharedPreferences, so the result is the same, but without the flexbility to split to multiple preference files, that is offered by getSharedPreferences(). Sharing the preferences between apps using a MODE_WORLD_READABLE operation indicator is also something possible using getSharedPreferences(), but is rarely used.

恕我直言, getDefaultShared preferences()的可以放心使用而无需进入的,它们很容易拼写错误和混乱多个preference文件名称的混淆。

IMHO, getDefaultSharedPreferences() can be safely used without going into the confusion of multiple preference file names that are prone to typos and confusion.

如果一个人的一个很好的理由知道使用的 getShared preferences()的而不是 getDefaultShared preferences()的,请让我知道通过评论点击这里

If someone knows of a good reason to use getSharedPreferences() and not getDefaultSharedPreferences(), please let me know by commenting here.

这篇关于在使用时getDefaultShared preferences和getShared preferences的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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