Android 持久层开发主要4个部分
1 sharedperferences通过键值2元组存储简单数据
2 文件存储
3 内置的SQLite
4 ContentProvide自定义抽象接口
sharedperferences类似于hashtable, 不过它是存在android的文件系统里的,
只要程序没有卸载这些程序存在
取得sharedperferences用getSharedPreferences 方法
官方文档如下
public abstract SharedPreferences getSharedPreferences (String name, int mode)
Retrieve and hold the contents of the preferences file 'name', returning a SharedPreferences through which you can retrieve and modify its values.
Only one instance of the SharedPreferences object is returned to any callers for the same name, meaning they will see each other's edits as soon as they are made.
第2个参数可以是下面的四个值
* MODE_PRIVATE
* MODE_WORLD_READABLE
* MODE_WORLD_WRITEABLE
* MODE_MULTI_PROCESS
他们控制着访问SharedPreferences的权限
sharedperferences支持int, float, long, string, boolean5个数据类型
设置属性,可以参照如下例子:
Log.v("Debug", "SharedPreferenceStart");
SharedPreferences preferences = getSharedPreferences("TestFile", MODE_PRIVATE);
String strTest = preferences.getString("TestKey", "NOValue");
Log.v("Debug", strTest);
if(strTest.equals("NOValue"))
{
SharedPreferences.Editor editor = preferences.edit();
editor.putString("TestKey", "TestValue"); // value to store
editor.commit();
}
strTest = preferences.getString("TestKey", "NOValue");
Log.v("Debug", strTest);
Log.v("Debug", "SharedPreferenceEnd");