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.
[PS] 两条准则, a .是否要做IPC, 共享数据 b. 数据的性质(大小,类型)
Your data storage options are the following:
-
1.
Shared Preferences
- Store private primitive data in key-value pairs.
-
The
SharedPreferences
class provides a general framework that allows you to save and retrieve persistent key-value pairs of primitive data types. You can useSharedPreferences
to save any primitive data: booleans, floats, ints, longs, and strings. This data will persist across user sessions (even if your application is killed).
- SharedPreferences也是一种轻量的数据存储方式,它的本质是基于XML文件存储key-value键值对数据,通常用来存储一些简单的配置信息。其存储位置在/data/data/<包名>/shared_prefs目录下(机器Root后可以命令行查看)。SharedPreferences对象本身只能获取数据而不支持存储和修改,存储修改是通过Editor对象实现。
- SharedPreferences对象与SQLite数据库相比,免去了创建数据库,创建表,写SQL语句等诸多操作,相对而言更加方便,简洁。但是SharedPreferences也有其自身缺陷,比如其只能存储boolean,int,float,long和String五种简单的数据类型,比如其无法进行条件查询等。所以不论SharedPreferences的数据存储操作是如何简单,它也只能是存储方式的一种补充,而无法完全替代如SQLite数据库这样的其他数据存储方式。 2. Internal Storage
- Store private data on the device memory.
- You can save files directly on the device's internal storage. By default, files saved to the internal storage are private to your application and other applications cannot access them (nor can the user). When the user uninstalls your application, these files are removed.
-
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.
- ContentProvide可以把数据暴露给其他应用 3. External Storage
- Store public data on the shared external storage.
- File存SD卡 4. SQLite Databases
- Store structured data in a private database.
- 轻量级的数据库 5. Network Connection
- Store data on the web with your own network server.
- SaaS <——> Client
- You can use the network (when it's available) to store and retrieve data on your own web-based services.
参考: http://developer.android.com/intl/zh-CN/guide/topics/data/data-storage.html