Data Storage(数据存储)
Store application data in databases, files, or preferences, in internal or removeable storage. You can also add a data backup service to let users store and recover application and system data.(Android应用中的数据以数据库,文件,preferences等形式存储在手机内部存储器或者可移动存储器。也可以添加一个数据备份服务让使用者可以备份和恢复应用和系统的数据。
Storage Options(存储选项)
Android provides several options for you to save persistent application data. The solution you choose depends on your specific needs, such as whether the data should be private to your application or accessible to other applications (and the user) and how much space your data requires.(Android系统为开发者提供了几种用于存储持久的应用程序数据,开发者根据开发的具体需求选择不同的方式去存储数据,比如,根据需要存储的数据对这个应用是私有的呢还是可以与其他应用(和其他使用者)共享以及这些数据的大小。
Your data storage options are the following:(可供选择的方式如下:)
Shared Preferences(共享参数)
Store private primitive data in key-value pairs.(以键值对的形式存储私人的原始数据)
Internal Storage(内部存储)
Store private data on the device memory.(存储私有数据在设备内部存储器)
External Storage(外部存储)
Store public data on the shared external storage.(存储公共类型的数据在外部存储)
SQLite Databases(sqlite 数据库)
Store structured data in a private database.(存储机构化的数据在私有数据库)
Network Connection(网络连接)
Store data on the web with your own network server.(依靠自己的网络服务存储数据在网络上)
Android provides a way for you to expose even your private data to other applications — with a content provider. A content provider is an optional component that exposes read/write access to your application data, subject to whatever restrictions you want to impose. For more information about using content providers, see the Content Providers documentation.(Android提供了一种可以让应用自身私有的数据暴露给其他应用的方法---内容提供者。内容提供者是一个根据你提供的限制条件使你应用数据暴露可读写的可选择组件。更多有关使用内容提供者的信息可以查看内容提供者文档)
Using Shared Preferences(内容提供者)
To get a SharedPreferences object for your application, use one of two methods:
使用以下两种方法之一去获取一个内容提供者对象
getSharedPreferences()
- Use this if you need multiple preferences files identified by name, which you specify with the first parameter.(如果你需要获取多个preferences文件通过名称(第一个参数)标识的指定,使用getSharedpreferences()方法。)
getPreferences()
-Use this if you need only one preferences file for your Activity. Because this will be the only preferences file for your Activity, you don't supply a name.(如果你只需为你的activity获取一个的preferences文件使用getPreferences()方法。因为这个文件是你的activity的唯一一个preferences文件)
To write values(写入值):
Call edit() to get a SharedPreferences.Editor.内容提供者对象调用edit()方法获取SharedPreferences.Editor对象
Add values with methods such as putBoolean() and putString().使用这些方法存储不同基础类型的数据
Commit the new values with commit()最后提交数据
To read values(读取值):
use SharedPreferences methods such as getBoolean() and getString().使用这些对应方法读取不同类型的存储数值。
Here is an example that saves a preference for silent keypress mode in a calculator(这里是一个使用SharedPreferences来保存一个计算器选择按键为静音模式的例子):
public class Calc extends Activity {
public static final String PREFS_NAME = "MyPrefsFile";
@Override
protected void onCreate(Bundle state){
super.onCreate(state);
. . .
// Restore preferences
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
boolean silent = settings.getBoolean("silentMode", false);
setSilent(silent);
}
@Override
protected void onStop(){
super.onStop();
// We need an Editor object to make preference changes.
// All objects are from android.context.Context
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("silentMode", mSilentMode);
// Commit the edits!
editor.commit();
}
}