如何从 Android 中的 PreferenceActivity 获取 SharedPreferences? [英] How do I get the SharedPreferences from a PreferenceActivity in Android?

查看:26
本文介绍了如何从 Android 中的 PreferenceActivity 获取 SharedPreferences?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 PreferenceActivity 来显示我的应用程序的一些设置.我正在通过 xml 文件扩充设置,以便我的 onCreate(和完整的类方法)如下所示:

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

PreferenceActivity 的 javadoc PreferenceFragment表示

The javadoc of PreferenceActivity PreferenceFragment states that

这些首选项将在用户与其交互时自动保存到 SharedPreferences.要检索此活动中的首选项层次结构将使用的 SharedPreferences 实例,请使用与此活动位于同一包中的上下文调用 getDefaultSharedPreferences(android.content.Context).

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.

但是我如何在另一个活动中获得 SharedPreference 的名称?我只能打电话

But how I get the name of the SharedPreference in another Activity? I can only call

getSharedPreferences(name, mode)

在其他活动中,但我需要 PreferenceActivity 使用的 SharedPreference 的名称.名称是什么或如何检索?

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?

推荐答案

import android.preference.PreferenceManager;
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
// then you use
prefs.getBoolean("keystring", true);

更新

根据共享首选项 |Android 开发者教程(第 13 部分) 作者:Sai Geetha M N,

According to Shared Preferences | Android Developer Tutorial (Part 13) by Sai Geetha M N,

许多应用程序可能会提供一种方法来捕获用户在特定应用程序或活动的设置.为支持为此,Android 提供了一组简单的 API.

Many applications may provide a way to capture user preferences on the settings of a specific application or an activity. For supporting this, Android provides a simple set of APIs.

首选项通常是名称值对.它们可以存储为跨应用程序中各种活动的共享首选项"(注意目前它不能跨进程共享).或者它可以是需要存储特定于活动的东西.

Preferences are typically name value pairs. They can be stored as "Shared Preferences" across various activities in an application (note currently it cannot be shared across processes). Or it can be something that needs to be stored specific to an activity.

  1. 共享首选项:应用程序的所有组件(活动、服务等)都可以使用共享首选项.

  1. 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.

共享偏好:

Context 类的getSharedPreferences 方法的帮助下管理共享首选项.首选项存储在默认文件 (1) 中,或者您可以指定文件名 (2) 用于引用首选项.

The shared preferences are managed with the help of getSharedPreferences method of the Context 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) 推荐方式使用默认模式,不指定文件名

(1) The recommended way is to use by the default mode, without specifying the file name

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);

(2) 这里是指定文件名时获取实例的方法

(2) Here is how you get the instance when you specify the file name

public static final String PREF_FILE_NAME = "PrefFile";
SharedPreferences preferences = getSharedPreferences(PREF_FILE_NAME, MODE_PRIVATE);

MODE_PRIVATE 是首选项的操作模式.这是默认模式,意味着只有调用应用程序才能访问创建的文件.支持的其他两种模式是 MODE_WORLD_READABLEMODE_WORLD_WRITEABLE.在MODE_WORLD_READABLE 中,其他应用程序可以读取创建的文件但不能修改它.在 MODE_WORLD_WRITEABLE 的情况下,其他应用程序也对创建的文件具有写入权限.

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 are MODE_WORLD_READABLE and MODE_WORLD_WRITEABLE. In MODE_WORLD_READABLE other application can read the created file but can not modify it. In case of MODE_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:

int storedPreference = preferences.getInt("storedInt", 0);

要在首选项文件中存储值,必须使用SharedPreference.Editor 对象.EditorSharedPreference 类中的嵌套接口.

To store values in the preference file SharedPreference.Editor object has to be used. Editor is a nested interface in the SharedPreference class.

SharedPreferences.Editor editor = preferences.edit();
editor.putInt("storedInt", storedPreference); // value to store
editor.commit();

Editor 还支持诸如 remove()clear() 之类的方法来从文件中删除首选项值.

Editor also supports methods like remove() and clear() to delete the preference values from the file.

活动偏好:

共享首选项可以被其他应用程序组件使用.但是,如果您不需要与其他组件共享首选项并希望拥有活动私有首选项,则可以借助活动的 getPreferences() 方法来实现.getPreference 方法使用 getSharedPreferences() 方法和活动类的名称作为首选项文件名.

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. The getPreference method uses the getSharedPreferences() method with the name of the activity class for the preference file name.

以下是获取偏好的代码

SharedPreferences preferences = getPreferences(MODE_PRIVATE);
int storedPreference = preferences.getInt("storedInt", 0);

存储值的代码也与共享首选项的情况相同.

The code to store values is also the same as in case of shared preferences.

SharedPreferences preferences = getPreference(MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putInt("storedInt", storedPreference); // value to store
editor.commit();

您还可以使用其他方法,例如将活动状态存储在数据库中.注意 Android 还包含一个名为 android.preference 的包.该包定义了实现应用程序首选项 UI 的类.

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.

要查看更多示例,请查看 Android 的数据存储 在开发者网站上发布.

To see some more examples check Android's Data Storage post on developers site.

这篇关于如何从 Android 中的 PreferenceActivity 获取 SharedPreferences?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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