Most visited

Recently visited

Added in API level 1

PreferenceManager

public class PreferenceManager
extends Object

java.lang.Object
    android.preference.PreferenceManager


用于帮助从活动或XML创建 Preference层次结构。

在大多数情况下,客户应使用 addPreferencesFromIntent(Intent)addPreferencesFromResource(int)

也可以看看:

摘要(Summary)

Nested classes

interface PreferenceManager.OnActivityDestroyListener

当容器的活动被销毁时将被调用的类的接口定义。

interface PreferenceManager.OnActivityResultListener

当容器的活动收到活动结果时将调用的类的接口定义。

interface PreferenceManager.OnActivityStopListener

当容器的活动停止时将被调用的类的接口定义。

常量(Constants)

String KEY_HAS_SET_DEFAULT_VALUES

String METADATA_KEY_PREFERENCES

它的XML偏好层次结构的活动元数据键。

公共方法(Public methods)

PreferenceScreen createPreferenceScreen(Context context)
Preference findPreference(CharSequence key)

根据密钥找到 Preference

static SharedPreferences getDefaultSharedPreferences(Context context)

获取一个SharedPreferences实例,该实例指向给定上下文中的首选项框架使用的默认文件。

static String getDefaultSharedPreferencesName(Context context)

返回用于存储默认共享首选项的名称。

SharedPreferences getSharedPreferences()

获取由此管理的首选项将使用的SharedPreferences实例。

int getSharedPreferencesMode()

返回由此管理的首选项将使用的SharedPreferences文件的当前模式。

String getSharedPreferencesName()

返回由此管理的首选项将使用的SharedPreferences文件的当前名称。

boolean isStorageDefault()

指示此类内部使用的存储位置是否由托管 Context提供的默认 Context

boolean isStorageDeviceProtected()

指示此类内部使用的存储位置是否受设备保护存储支持。

static void setDefaultValues(Context context, String sharedPreferencesName, int sharedPreferencesMode, int resId, boolean readAgain)

类似于 setDefaultValues(Context, int, boolean)但允许客户端提供共享首选项文件的文件名和模式。

static void setDefaultValues(Context context, int resId, boolean readAgain)

通过读取每个 Preference项目的 android:defaultValue属性定义的值来设置XML首选项文件的默认值。

void setSharedPreferencesMode(int sharedPreferencesMode)

设置由此管理的首选项将使用的SharedPreferences文件的模式。

void setSharedPreferencesName(String sharedPreferencesName)

设置由此管理的首选项将使用的SharedPreferences文件的名称。

void setStorageDefault()

将此类内部使用的存储位置设置为主机 Context提供的默认 Context

void setStorageDeviceProtected()

将此类内部使用的存储位置明确设置为受设备保护的存储。

继承方法(Inherited methods)

From class java.lang.Object

常量(Constants)

KEY_HAS_SET_DEFAULT_VALUES

Added in API level 1
String KEY_HAS_SET_DEFAULT_VALUES

常量值:“_has_set_default_values”

METADATA_KEY_PREFERENCES

Added in API level 1
String METADATA_KEY_PREFERENCES

它的XML偏好层次结构的活动元数据键。

常量值:“android.preference”

公共方法(Public methods)

createPreferenceScreen

Added in API level 1
PreferenceScreen createPreferenceScreen (Context context)

参数(Parameters)
context Context
返回(Returns)
PreferenceScreen

findPreference

Added in API level 1
Preference findPreference (CharSequence key)

根据其密钥找到 Preference

参数(Parameters)
key CharSequence: The key of the preference to retrieve.
返回(Returns)
Preference The Preference with the key, or null.

也可以看看:

getDefaultSharedPreferences

Added in API level 1
SharedPreferences getDefaultSharedPreferences (Context context)

获取一个SharedPreferences实例,该实例指向给定上下文中的首选项框架使用的默认文件。

参数(Parameters)
context Context: The context of the preferences whose values are wanted.
返回(Returns)
SharedPreferences A SharedPreferences instance that can be used to retrieve and listen to values of the preferences.

getDefaultSharedPreferencesName

Added in API level 24
String getDefaultSharedPreferencesName (Context context)

返回用于存储默认共享首选项的名称。

参数(Parameters)
context Context
返回(Returns)
String

也可以看看:

getSharedPreferences

Added in API level 1
SharedPreferences getSharedPreferences ()

获取由此管理的首选项将使用的SharedPreferences实例。

返回(Returns)
SharedPreferences A SharedPreferences instance pointing to the file that contains the values of preferences that are managed by this.

getSharedPreferencesMode

Added in API level 1
int getSharedPreferencesMode ()

返回由此管理的首选项将使用的SharedPreferences文件的当前模式。

返回(Returns)
int The mode that can be passed to getSharedPreferences(String, int).

也可以看看:

getSharedPreferencesName

Added in API level 1
String getSharedPreferencesName ()

返回由此管理的首选项将使用的SharedPreferences文件的当前名称。

返回(Returns)
String The name that can be passed to getSharedPreferences(String, int).

也可以看看:

isStorageDefault

Added in API level 24
boolean isStorageDefault ()

指示此类内部使用的存储位置是由主机 Context提供的默认 Context

返回(Returns)
boolean

也可以看看:

isStorageDeviceProtected

Added in API level 24
boolean isStorageDeviceProtected ()

指示此类内部使用的存储位置是否受设备保护存储支持。

返回(Returns)
boolean

也可以看看:

setDefaultValues

Added in API level 1
void setDefaultValues (Context context, 
                String sharedPreferencesName, 
                int sharedPreferencesMode, 
                int resId, 
                boolean readAgain)

类似于 setDefaultValues(Context, int, boolean)但允许客户端提供共享首选项文件的文件名和模式。

参数(Parameters)
context Context: The context of the shared preferences.
sharedPreferencesName String: A custom name for the shared preferences file.
sharedPreferencesMode int: The file creation mode for the shared preferences file, such as MODE_PRIVATE or MODE_PRIVATE
resId int: The resource ID of the preference XML file.
readAgain boolean: Whether to re-read the default values. If false, this method will set the default values only if this method has never been called in the past (or if the KEY_HAS_SET_DEFAULT_VALUES in the default value shared preferences file is false). To attempt to set the default values again bypassing this check, set readAgain to true.

注意:这不会将首选项重置为其默认值。 对于该功能,使用getDefaultSharedPreferences(Context)并清除它,然后调用此方法,并将此参数设置为true。

也可以看看:

setDefaultValues

Added in API level 1
void setDefaultValues (Context context, 
                int resId, 
                boolean readAgain)

通过读取每个Preference项目的android:defaultValue属性定义的值来设置XML首选项文件的默认值。 这应该由应用程序的主要活动调用。

参数(Parameters)
context Context: The context of the shared preferences.
resId int: The resource ID of the preference XML file.
readAgain boolean: Whether to re-read the default values. If false, this method sets the default values only if this method has never been called in the past (or if the KEY_HAS_SET_DEFAULT_VALUES in the default value shared preferences file is false). To attempt to set the default values again bypassing this check, set readAgain to true.

注意:这不会将首选项重置为其默认值。 对于该功能,使用getDefaultSharedPreferences(Context)并清除它,然后调用此方法,并将此参数设置为true。

setSharedPreferencesMode

Added in API level 1
void setSharedPreferencesMode (int sharedPreferencesMode)

设置由此管理的首选项将使用的SharedPreferences文件的模式。

参数(Parameters)
sharedPreferencesMode int: The mode of the SharedPreferences file.

也可以看看:

setSharedPreferencesName

Added in API level 1
void setSharedPreferencesName (String sharedPreferencesName)

设置由此管理的首选项将使用的SharedPreferences文件的名称。

参数(Parameters)
sharedPreferencesName String: The name of the SharedPreferences file.

也可以看看:

setStorageDefault

Added in API level 24
void setStorageDefault ()

将此类内部使用的存储位置设置为主机 Context提供的默认 Context

setStorageDeviceProtected

Added in API level 24
void setStorageDeviceProtected ()

将此类内部使用的存储位置明确设置为受设备保护的存储。

在直接引导的设备上,存储在此位置的数据使用绑定到物理设备的密钥进行加密,并且可以在设备成功引导后立即访问,无论是 用户使用凭据进行身份验证 之前还是之后 (例如锁定模式或PIN)。

由于受到设备保护的数据在没有用户身份验证的情况下可用,因此应谨慎限制使用此上下文存储的数据。 例如,强烈建议不要在设备保护区域中存储敏感身份验证令牌或密码。

也可以看看:

Hooray!