使数据持久化的机器人机器人、持久、数据

2023-09-12 00:14:11 作者:好久不见,我已胖若两人

在我的应用程序,也有一些应用程序特定的设置,这应该是提供给我,下一次当我的应用程序启动。

In my application,there are some application specific settings, which should be available to me , next time when my application starts up.

在换句话说,我希望数据可跨应用程序周期的会议。

In other words i want the data to be available across the sessions of an application cycle.

这可以在不使用的数据库(源码)实现的。

Can this be achieved without using database(sqlite).

推荐答案

许多应用程序可以提供一种方法来捕获特定应用程序或活动的设置,用户preferences。为了支持这一点,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是典型的名称值对。它们可以存储为共享preferences在各种活动中的应用程序(注意目前不能在进程间共享)。或者它可以是一些需要被存储到特定的活性。

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.

共用preferences:共享preferences可以使用所有部件(活动,服务等)关闭应用

Shared Preferences: The shared preferences can be used by all the components (activities, services etc) off the applications.

活动处理preferences:这些preferences只能与在活动中使用,不能使用的应用程序的其它部件

Activity handled preferences: These preferences can only be used with in the activity and can not be used by other components of the application.

共享preferences:

共享preferences都与上下文的 getShared preferences 的帮助方法管理类。的preferences存储在默认文件(1)也可以指定(2)被用来指preferences一个文件名。

The shared preferences are managed with the help of the 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) 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 的操作模式为preferences。它是默认模式和装置所创建的文件将通过仅调用应用程序进行访问。支持另外两种模式是 MODE_WORLD_READABLE MODE_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 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)推荐的方法是默认的模式中使用,而不指定文件名:

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

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);

最后,一旦你有preferences例如,这里是你如何能从preferences检索存储的值

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

要存储值在preference文件共享preference.Editor 对象必须使用。 编辑共享preference 类的嵌套接口。

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

编辑器还支持像方法删除()明确()删除从preference值该文件。

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

活动preferences:

共享preferences可以由其他应用程序组件。但是,如果你不需要共享preferences与其他组件,并希望有活动专用preferences。你可以做到这一点与获取preferences()活动的方法的帮助。该获取preference 方法使用 getShared preferences()方法与活动类的名称为preference文件名。

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.

以下是code得到preferences:

Following is the code to get preferences:

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

在code储存的值也相同的情况下共享preferences的。

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

您也可以使用其他的方法,如储存活动状态的数据库。注意的Andr​​oid还含有一种叫机器人。preference 包。该包定义类来实现应用程序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.

要看到更多的例子检查Android的数据存储后开发商的网站。

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