如何从 Android 中的 PreferenceActivity 获取 SharedPreferences?
我正在使用 PreferenceActivity 来显示我的应用程序的一些设置。我通过 xml 文件扩充设置,以便我的 onCreate (和完整的类方法)如下所示:
public class FooActivity extends PreferenceActivity {
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
addPreferencesFromResource(R.xml.preference);
}
}
PreferenceActivity PreferenceFragment 指出
当用户与它们交互时,这些首选项将自动保存到 SharedPreferences 中。要检索此 Activity 中的首选项层次结构将使用的 SharedPreferences 实例,请使用与此 Activity 位于同一包中的上下文调用 getDefaultSharedPreferences(android.content.Context)。
但是如何在另一个 Activity 中获取 SharedPreference 的名称呢?我只能调用
getSharedPreferences(name, mode)
其他活动,但我需要 PreferenceActivity 使用的 SharedPreference 的名称。名称是什么或者如何检索它?
I am using a PreferenceActivity to show some settings for my application. I am inflating the settings via a xml file so that my onCreate (and complete class methods) looks like this:
public class FooActivity extends PreferenceActivity {
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
addPreferencesFromResource(R.xml.preference);
}
}
The javadoc of PreferenceActivity PreferenceFragment states that
These preferences will automatically save to SharedPreferences as the user interacts with them. To retrieve an instance of SharedPreferences that the preference hierarchy in this activity will use, call getDefaultSharedPreferences(android.content.Context) with a context in the same package as this activity.
But how I get the name of the SharedPreference in another Activity? I can only call
getSharedPreferences(name, mode)
in the other activity but I need the name of the SharedPreference which was used by the PreferenceActivity. What is the name or how can i retrieve it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
更新
根据共享首选项 | Android 开发者教程(第 13 部分) 作者:Sai Geetha MN,
共享首选项:共享首选项可供应用程序的所有组件(活动、服务等)使用。
共享
活动处理的首选项:这些首选项只能在特定的活动中使用,不能被应用程序的其他组件使用。
活动处理的首选项:这些
共享首选项:
共享首选项通过
Context
类的getSharedPreferences
方法进行管理。首选项存储在默认文件 (1) 中,或者您可以指定用于引用首选项的文件名 (2)。(1)推荐方式是按默认模式使用,不指定文件名
(2)下面是指定文件名时获取实例的方式
MODE_PRIVATE
是首选项的操作模式。这是默认模式,意味着创建的文件将仅由调用应用程序访问。支持的其他两种模式是MODE_WORLD_READABLE
和MODE_WORLD_WRITEABLE
。在MODE_WORLD_READABLE
中,其他应用程序可以读取创建的文件,但不能修改它。在MODE_WORLD_WRITEABLE
的情况下,其他应用程序也对所创建的文件具有写入权限。最后,一旦您拥有了首选项实例,您可以通过以下方式从首选项检索存储的值:
在首选项文件
SharedPreference.Editor<中存储值必须使用 /code> 对象。
Editor
是SharedPreference
类中的嵌套接口。编辑器还支持
remove()
和clear()
等方法从文件中删除首选项值。活动首选项:
共享首选项可供其他应用程序组件使用。但是,如果您不需要与其他组件共享首选项并希望拥有活动私有首选项,则可以借助活动的 getPreferences() 方法来实现。
getPreference
方法使用getSharedPreferences()
方法,并将活动类的名称作为首选项文件名。以下是获取首选项的代码。
存储值的代码也与共享首选项的情况相同。
您还可以使用其他方法,例如将活动状态存储在数据库中。注意 Android 还包含一个名为
android.preference
的包。该包定义了用于实现应用程序首选项 UI 的类。要查看更多示例,请查看 Android 的 数据存储 帖子开发者网站。
Update
According to Shared Preferences | Android Developer Tutorial (Part 13) by Sai Geetha M N,
Shared Preferences: The shared preferences can be used by all the components (activities, services etc) of the applications.
Activity handled preferences: These preferences can only be used within the particular activity and can not be used by other components of the application.
Shared Preferences:
The shared preferences are managed with the help of
getSharedPreferences
method of theContext
class. The preferences are stored in a default file (1) or you can specify a file name (2) to be used to refer to the preferences.(1) The recommended way is to use by the default mode, without specifying the file name
(2) Here is how you get the instance when you specify the file name
MODE_PRIVATE
is the operating mode for the preferences. It is the default mode and means the created file will be accessed by only the calling application. Other two modes supported areMODE_WORLD_READABLE
andMODE_WORLD_WRITEABLE
. InMODE_WORLD_READABLE
other application can read the created file but can not modify it. In case ofMODE_WORLD_WRITEABLE
other applications also have write permissions for the created file.Finally, once you have the preferences instance, here is how you can retrieve the stored values from the preferences:
To store values in the preference file
SharedPreference.Editor
object has to be used.Editor
is a nested interface in theSharedPreference
class.Editor also supports methods like
remove()
andclear()
to delete the preference values from the file.Activity Preferences:
The shared preferences can be used by other application components. But if you do not need to share the preferences with other components and want to have activity private preferences you can do that with the help of
getPreferences()
method of the activity. ThegetPreference
method uses thegetSharedPreferences()
method with the name of the activity class for the preference file name.Following is the code to get preferences
The code to store values is also the same as in case of shared preferences.
You can also use other methods like storing the activity state in database. Note Android also contains a package called
android.preference
. The package defines classes to implement application preferences UI.To see some more examples check Android's Data Storage post on developers site.
如果您无权访问
getDefaultSharedPreferenes()
,则可以使用getSharedPreferences(name, mode)
代替,只需传入正确的名称即可。Android 创建此名称(可能基于您项目的包名称?)。您可以通过将以下代码放入
SettingsActivity
onCreate()
中,并查看preferencesName
是什么来获取它。该字符串应类似于
com.example.projectname_preferences
。在项目中的某个位置进行硬编码,并将其传递给 getSharedPreferences() ,然后就可以开始了。If you don't have access to
getDefaultSharedPreferenes()
, you can usegetSharedPreferences(name, mode)
instead, you just have to pass in the right name.Android creates this name (possibly based on the package name of your project?). You can get it by putting the following code in a
SettingsActivity
onCreate()
, and seeing whatpreferencesName
is.The string should be something like
com.example.projectname_preferences
. Hard code that somewhere in your project, and pass it in togetSharedPreferences()
and you should be good to go.首先声明这些方法..
然后当你想要放置一个 pref 时调用这个:
当你想要获取一个 pref 时调用这个:
或者你可以使用这个对象https://github.com/kcochibili/TinyDB--Android-Shared-Preferences-Turbo
这进一步简化了一切
示例:
Declare these methods first..
Then call this when you want to put a pref:
call this when you want to get a pref:
Or you can use this object https://github.com/kcochibili/TinyDB--Android-Shared-Preferences-Turbo
which simplifies everything even further
Example:
必须到处传递上下文真的很烦我。代码变得过于冗长且难以管理。我在每个项目中都这样做......
并将其设置在主要活动中创建,
所有首选项键都应该与语言无关,我很震惊没有人提到这一点。
现在用一行代码非常简单地调用它
having to pass context around everywhere is really annoying me. the code becomes too verbose and unmanageable. I do this in every project instead...
and set it in the main activity create
also all preference keys should be language independent, I'm shocked nobody has mentioned that.
now call it very simply like this in one line of code
如果你有一个复选框并且你想在任何 java 文件中获取它的值,即 true / false--
使用--
if you have a checkbox and you would like to fetch it's value ie true / false in any java file--
Use--
尝试以下对我有用的源代码
Try following source code it worked for me