SharedPreferences用法

来源:互联网 发布:男生挎包 知乎 编辑:程序博客网 时间:2024/05/22 19:34

转自:http://www.cnblogs.com/wisekingokok/archive/2011/09/16/2177833.html

除了SQLite数据库外,SharedPreferences也是一种轻型的数据存储方式,它的本质是基于XML文件存储key-value键值对数据,通常用来存储一些简单的配置信息。其存储位置在/data/data/<包名>/shared_prefs目录下。SharedPreferences对象本身只能获取数据而不支持存储和修改,存储修改是通过Editor对象实现。实现SharedPreferences存储的步骤如下:

  一、根据Context获取SharedPreferences对象

  二、利用edit()方法获取Editor对象。

  三、通过Editor对象存储key-value键值对数据。

  四、通过commit()方法提交数据。


重要的常量:

public static final int MODE_PRIVATE

Since: API Level 1

File creation mode: the default mode, where the created file can only be accessed by the calling application (or all applications sharing the same user ID).

public static final int MODE_WORLD_READABLE

Since: API Level 1

File creation mode: allow all other applications to have read access to the created file.

public static final int MODE_WORLD_WRITEABLE

Since: API Level 1

File creation mode: allow all other applications to have write access to the created file.

public static final int MODE_MULTI_PROCESS

Since: API Level 11

SharedPreference loading flag: when set, the file on disk will be checked for modification even if the shared preferences instance is already loaded in this process. This behavior is sometimes desired in cases where the application has multiple processes, all writing to the same SharedPreferences file. Generally there are better forms of communication between processes, though.

This was the legacy (but undocumented) behavior in and before Gingerbread (Android 2.3) and this flag is implied when targetting such releases. For applications targetting SDK versions greater than Android 2.3, this flag must be explicitly set if desired.


  具体实现代码如下:

复制代码
 1 public class MainActivity extends Activity {
2
@Override
3 public void
onCreate(Bundle savedInstanceState) {
4 super
.onCreate(savedInstanceState);
5
setContentView(R.layout.main);
6

7 //获取SharedPreferences对象

8 Context ctx = MainActivity.this;
9 SharedPreferences sp = ctx.getSharedPreferences("SP"
, MODE_PRIVATE);
10 //存入数据

11 Editor editor = sp.edit();
12 editor.putString("STRING_KEY", "string"
);
13 editor.putInt("INT_KEY", 0
);
14 editor.putBoolean("BOOLEAN_KEY", true
);
15
editor.commit();
16

17 //返回STRING_KEY的值

18 Log.d("SP", sp.getString("STRING_KEY", "none"));
19 //如果NOT_EXIST不存在,则返回值为"none"

20 Log.d("SP", sp.getString("NOT_EXIST", "none"));
21
}
22 }
复制代码

   这段代码执行过后,即在/data/data/com.test/shared_prefs目录下生成了一个SP.xml文件,一个应用可以创建多个这样的xml文件。如图所示: 

   SP.xml文件的具体内容如下:

1 <?xml version='1.0' encoding='utf-8' standalone='yes' ?>
2 <map>
3 <string name="STRING_KEY">string</string>
4 <int name="INT_KEY" value="0" />
5 <boolean name="BOOLEAN_KEY" value="true" />
6 </map>

  在程序代码中,通过getXXX方法,可以方便的获得对应Key的Value值,如果key值错误或者此key无对应value值,SharedPreferences提供了一个赋予默认值的机会,以此保证程序的健壮性。如下图运行结果中因为并无值为"NOT_EXIST"的Key,所以Log打印出的是其默认值:“none”。在访问一个不存在key值这个过程中,并无任何异常抛出。  

  SharedPreferences对象与SQLite数据库相比,免去了创建数据库,创建表,写SQL语句等诸多操作,相对而言更加方便,简洁。但是SharedPreferences也有其自身缺陷,比如其职能存储boolean,int,float,long和String五种简单的数据类型,比如其无法进行条件查询等。所以不论SharedPreferences的数据存储操作是如何简单,它也只能是存储方式的一种补充,而无法完全替代如SQLite数据库这样的其他数据存储方式。


如果要读取配置文件信息,只需要直接使用SharedPreferences对象的getXXX()方法即可,而如果要写入配置信息,则必须先调用SharedPreferences对象的edit()方法,使其处于可编辑状态,然后再调用putXXX()方法写入配置信息,最后调用commit()方法提交更改后的配置文件。