在 Android 中进行“应用程序设置”的最佳方法是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3570690/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What's the best way to do "application settings" in Android?
提问by Tim Almond
I'd like to store some application settings (like the URL of an API, and some settings for testing) for an Android application.
我想为 Android 应用程序存储一些应用程序设置(如 API 的 URL,以及一些用于测试的设置)。
I mostly work as a .NETdeveloper, so I'm used to using the file app.config
for this purpose. What's a good way to do it in Android?
我主要从事.NET开发人员的工作,因此我习惯于app.config
为此目的使用该文件。在 Android 中这样做的好方法是什么?
回答by Pentium10
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.
许多应用程序可以提供一种方法来捕获用户对特定应用程序或活动设置的偏好。为了支持这一点,Android 提供了一组简单的 API。
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.
首选项通常是名称值对。它们可以跨应用程序中的各种活动存储为“共享首选项”(注意目前它不能跨进程共享)。或者它可以是需要存储特定于活动的东西。
Shared Preferences: The shared preferences can be used by all the components (activities, services etc) off the applications.
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.
getSharedPreferences
在Context
类的方法的帮助下管理共享首选项。首选项存储在默认文件 (1) 中,或者您可以指定用于引用首选项的文件名 (2)。
(1) Here is how you get the instance when you specify the file name
(1) 指定文件名时获取实例的方法如下
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.
MODE_PRIVATE
是首选项的操作模式。这是默认模式,意味着只有调用应用程序才能访问创建的文件。支持的其他两种模式是MODE_WORLD_READABLE
和MODE_WORLD_WRITEABLE
。在MODE_WORLD_READABLE
其他应用程序中可以读取创建的文件但不能修改它。如果MODE_WORLD_WRITEABLE
其他应用程序也对创建的文件具有写权限。
(2) The recommended wayis to use by the default mode, without specifying the file name
(2)推荐使用默认模式,不指定文件名
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
Finally, once you have the preferences instance, here is how you can retrieve the stored valuesfrom the preferences:
最后,一旦你有了首选项实例,这里是你如何从首选项中检索存储的值:
int storedPreference = preferences.getInt("storedInt", 0);
To store valuesin the preference file SharedPreference.Editor
object has to be used. Editor
is the nested interface of the SharedPreference
class.
必须使用首选项文件对象来存储值SharedPreference.Editor
。Editor
是类的嵌套接口SharedPreference
。
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.
编辑器还支持类似的方法remove()
,并clear()
以从文件中删除喜好值。
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.
其他应用程序组件可以使用共享首选项。但是如果您不需要与其他组件共享首选项并希望拥有活动的私人首选项。您可以借助getPreferences()
活动的方法来做到这一点。该getPreference
方法使用getSharedPreferences()
带有活动类名称的方法作为首选项文件名。
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.
您还可以使用其他方法,例如将活动状态存储在数据库中。注意 Android 还包含一个名为android.preference
. 该包定义了实现应用程序首选项 UI 的类。
To see some more examples check Android's Data Storagepost on developers site.
要查看更多示例,请查看开发者网站上的Android数据存储帖子。