用户首选项文件与应用程序首选项文件

发布于 2024-09-06 20:56:06 字数 282 浏览 10 评论 0原文

我的 Android 应用程序有两种首选项:

1) 我在 res/xml/preferences.xml 中定义了用户首选项,以便用户可以使用 PreferenceActivity 管理他们的首选项。

2) 我想为我的应用程序的全局配置首选项定义另一个文件。

管理我的应用程序配置首选项的最佳方式是什么?我应该使用配置值创建另一个 XML 文件还是应该在 strings.xml 中指定这些配置值?管理配置首选项的最佳实践是什么?

My android application has two kinds of preferences:

1) I have user preferences defined in res/xml/preferences.xml so that users can manage their preferences with a PreferenceActivity.

2) I'd like to define another file for global configuration preferences of my app.

What is the best way to manage my app config preferences? Should I create another XML file with config values or should I specify those config values in strings.xml? What is the best practice for managing config preferences?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

辞旧 2024-09-13 20:56:06

首选项文件存储在项目的/res/xml/defaults.xml中,

它们存储在设备上您的应用程序文件夹中,例如

/data/data/com.your.pkg/default.prefs

尝试不要指定文件名,如您将在首选项文件方面遇到一些问题,就像这个OP在这个问题中遇到的问题: 我如何获得在 Android 中工作的首选项?

SharedPreferences preferences = PreferenceManager
                    .getDefaultSharedPreferences(context);

那么您可能需要查询

preferences.getString('weightPref', null);

许多应用程序可能提供一种方法来捕获用户对特定应用程序或活动的设置的首选项。为了支持这一点,Android 提供了一组简单的 API。

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

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

    共享

  2. 活动处理的首选项:这些首选项只能在活动中使用,不能被应用程序的其他组件使用。

    活动处理的首选项:这些首选项只能在活动中使用,不能被应用程序的其他组件使用

共享首选项

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

时如何获取实例

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

(1) 以下是当您指定文件名MODE_PRIVATE 是首选项的操作模式 。这是默认模式,意味着创建的文件将仅由调用应用程序访问。支持的其他两种模式是 MODE_WORLD_READABLEMODE_WORLD_WRITEABLE。在MODE_WORLD_READABLE中,其他应用程序可以读取创建的文件,但不能修改它。在 MODE_WORLD_WRITEABLE 的情况下,其他应用程序也对所创建的文件具有写入权限。

(2) 推荐的方式是使用默认模式,不指定文件名

SharedPreferences preferences = PreferencesManager.getDefaultSharedPreferences(context);

最后,一旦您有了首选项实例,您可以通过以下方式检索存储的值 来自首选项:

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

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

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

编辑器还支持 remove()clear() 等方法从文件中删除首选项值。

活动首选项

共享首选项可供其他应用程序组件使用。但是,如果您不需要与其他组件共享首选项并希望拥有活动私有首选项。您可以借助 Activity 的 getPreferences() 方法来完成此操作。 getPreference 方法使用 getSharedPreferences() 方法,并将活动类的名称作为首选项文件名。

以下是获取首选项的代码。

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

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

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

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

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

Preference files are not storead in project's /res/xml/defaults.xml

They are stored on the device in your application folder something like

/data/data/com.your.pkg/default.prefs

Try do not specify the file name, as you will have some problems with the preference files, like this OP had in this question: How do I get preferences to work in Android?

SharedPreferences preferences = PreferenceManager
                    .getDefaultSharedPreferences(context);

Then you will probably have to query

preferences.getString('weightPref', null);

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. Shared Preferences: The shared preferences can be used by all the components (activities, services etc) off the applications.

  2. Activity handled preferences: These preferences can only be used with in the 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 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) 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 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 mode 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.

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

SharedPreferences preferences = PreferencesManager.getDefaultSharedPreferences(context);

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

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

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

Editor also support methods like remove() and clear() to delete the preference value 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 activities 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.

Following is the code to get preferences

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

The code to store values is also 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();

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.

凝望流年 2024-09-13 20:56:06

让我们假设您的 xml 如下所示:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
   <CheckBoxPreference android:key="gpsOn" 
                    android:summaryOff="GPS is Off"
                    android:summaryOn="GPS is On" 
                    android:title="GPS State"></CheckBoxPreference>

我假设您使用 PreferenceActivity 从 xml 加载首选项。因此,在您的活动中,您执行 addPreferencesFromResource(R.xml.application_pref);

当您执行此操作时,默认情况下,所有值都存储在应用程序的默认 SharedPreference 中。您可以在任何地方访问这些首选项。因此,从其他一些活动/服务中只需执行以下操作:

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
boolean value = prefs.getBoolean("gpsOn", false);

就像您应该在 value 中获取用户的偏好。

Let us assume that your xml reads like this:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
   <CheckBoxPreference android:key="gpsOn" 
                    android:summaryOff="GPS is Off"
                    android:summaryOn="GPS is On" 
                    android:title="GPS State"></CheckBoxPreference>

I assume you use a PreferenceActivity to load the preferences from your xml. So in your activity you do addPreferencesFromResource(R.xml.application_pref);

When you do this, by default, all the values are stored in the default SharedPreference of the application. You can access these preferences in anywhere you want. So from some other activity/service just do:

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
boolean value = prefs.getBoolean("gpsOn", false);

And just like that you should get the user's preference in value.

北城挽邺 2024-09-13 20:56:06

为应用设置全局配置首选项的最佳方法可能是在 Android Manifest 中定义元数据元素

Probably the best way to set global configuration preferences for an app is to define meta-data elements in the Android Manifest

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文