Saving Data --- Saving Key-Value Sets(一)

来源:互联网 发布:8051 单片机有哪些 编辑:程序博客网 时间:2024/06/05 05:22

Most Android apps need to save data, even if only to save information about the app state during onPause() so the user’s progress is not lost. Most non-trivial apps also need to save user settings, and some apps must manage large amounts of information in files and databases. This class introduces you to the principal data storage options in Android, including:

  • Saving key-value pairs of simple data types in a shared preferences file
  • Saving arbitrary files in Android’s file system
  • Using databases managed by SQLite

绝大多数App都需要存储数据。主要有三种存储方式:
* 存储为键值对形式的shared preferences文件
* 存储为二进制文件
* 存储为数据库文件

Saving Key-Value Sets

If you have a relatively small collection of key-values that you’d like to save, you should use the SharedPreferences APIs. A SharedPreferences object points to a file containing key-value pairs and provides simple methods to read and write them. Each SharedPreferences file is managed by the framework and can be private or shared.

如果你有一些相对小的键值对集合需要存储,你应该使用SharedPreferences APIs。一个SharedPreferences对象指向了一个包含键值对集合的文件,并且提供了简单的方法去读取或者写入他们。Framework层管理着每个SharedPreferences文件,它可以是私有的或者共享的。

Get a Handle to a SharedPreferences

You can create a new shared preference file or access an existing one by calling one of two methods:

  • getSharedPreferences() — Use this if you need multiple shared preference files identified by name, which you specify with the first parameter. You can call this from any Context in your app.

  • getPreferences() — Use this from an Activity if you need to use only one shared preference file for the activity. Because this retrieves a default shared preference file that belongs to the activity, you don’t need to supply a name.

介绍了两个方法,简单来说,第一个是适用于整个程序中使用Context调用的,第二个是专属于某个Activity。

For example, the following code is executed inside a Fragment. It accesses the shared preferences file that’s identified by the resource string R.string.preference_file_key and opens it using the private mode so the file is accessible by only your app.

Context context = getActivity();SharedPreferences sharedPref = context.getSharedPreferences(        getString(R.string.preference_file_key), Context.MODE_PRIVATE);

When naming your shared preference files, you should use a name that’s uniquely identifiable to your app, such as “com.example.myapp.PREFERENCE_FILE_KEY”

介绍了创建SharedPreferences对象。

Caution: If you create a shared preferences file with MODE_WORLD_READABLE or MODE_WORLD_WRITEABLE, then any other apps that know the file identifier can access your data.

如果你使用 MODE_WORLD_READALBE 或者 MODE_WORLD_WRITEABLE 来创建一个shared preferences文件,那么其他的apps如果知道了文件标示符则页可以访问你的数据。

Write to Shared Preferences

To write to a shared preferences file, create a SharedPreferences.Editor by calling edit() on your SharedPreferences.

Pass the keys and values you want to write with methods such as putInt() and putString(). Then call commit() to save the changes. For example:

SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);SharedPreferences.Editor editor = sharedPref.edit();editor.putInt(getString(R.string.saved_high_score), newHighScore);editor.commit();

Read from Shared Preferences

To retrieve values from a shared preferences file, call methods such as getInt() and getString(), providing the key for the value you want, and optionally a default value to return if the key isn’t present. For example:

SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);int defaultValue = getResources().getInteger(R.string.saved_high_score_default);long highScore = sharedPref.getInt(getString(R.string.saved_high_score), defaultValue);
0 0
原创粉丝点击