下面通过SimplePreferenceDemo示例介绍具体说明SharedPreferences的文件保存位置和保存格式及其读取。
图7-1是SimplePreferenceDemo示例的用户界面。

图7-1 SimplePreferenceDemo示例的用户界面
在图7-1所示的界面中,用户在界面上输入信息,然后退出这个应用,即关闭Activity时将SharedPreferences进行保存。当在应用程序列表中找到这个应用重新开启时,保存在SharedPreferences的信息(刚刚输入的信息)将被读取出来,并重新呈现在用户界面上。
界面代码如代码清单7-7所示。
代码清单7-7 main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="//schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="姓名:"
>
</TextView>
<EditText
android:id="@+id/name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""
>
</EditText>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="身高:"
>
</TextView>
<EditText
android:id="@+id/height"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""
>
</EditText>
</LinearLayout>
主要实现代码如代码清单7-8所示。
代码清单7-8 SimplePreferenceDemo.java
package cn.com.farsight. SimplePreferenceDemo;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.EditText;
public class SharedPreferencesDemo extends Activity {
public static final String SETTING_INFOS = "SETTING_Infos";
public static final String NAME = "NAME";
public static final String HEIGHT = " HEIGHT ";
private EditText field_name; //接收用户名的组件
private EditText filed_height; //接收身高的组件
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//Find VIew
field_name =(EditText) findViewById(R.id.name);//首先获取用来输入用户名的组件
filed_ height = (EditText) findViewById(R.id.height); //同时也需要获取输入身高的组件
// Restore preferences
//获取一个SharedPreferences对象
SharedPreferences settings =
getSharedPreferences(SETTING_INFOS, 0);
String name = settings.getString(NAME, ""); //取出保存的NAME
String height = settings.getFloat(HEIGHT, "").toString(); //取出保存的HEIGHT
//Set value
field_name.setText(name); //将取出来的用户名赋予field_name
filed_ height.setText(height); //将取出来的身高赋予filed_height
}
@Override
protected void onStop(){
super.onStop();
//首先获取一个SharedPreferences对象
SharedPreferences settings =
getSharedPreferences(SETTING_INFOS, 0);
settings.edit()
.putString(NAME, field_name.getText().toString())
. putFloat(HEIGHT,
Float.parseFloat(filed_ height.getText().toString())
.commit();
} //将用户名和身高保存进去
}
通过上述代码可以看出,在onCreate中使用findViewByld得到两个EditView后,使用getSharedPreferences取得SharedPreferences对象settings,然后使用getString取得其中保存的值,后使用setText将其值设置为两个EditText的值。
而在程序运行onStop过程,也就是在程序退出时,首先使用getSharedPreferences得到settings;其次调用edit()方法使其处于可以编辑状态,并使用putString将两个EditText中的值保存起来;后使用commit()方法提交即可保存。
SharedPreferences保存到哪里去了?
SharedPreferences是以XML的格式以文件的方式自动保存的,在SimplePreferenceDemo示例运行后,DDMS中的File Explorer展开到/data/data,可以看到Android为每个应用程序建立了与包同名的目录,用来保存应用程序产生的数据,这些数据包括文件、SharedPreferences文件和数据库等;将目录展开到/data/data//shared_prefs下,以上面这个为例,可以看到一个叫做SETTING_Infos.xml的文件,如图7-2所示。

图7-2 SharedPreferences文件
将SETTING_Infos.xml导出至设备,其代码如代码清单7-9所示。
代码清单7-9 SaveSetting.xml
<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
<map>
<float name="Height" value="1.81" />
<string name="Name">Tom</string>
</map>
注意:Preferences只能在同一个包内使用,不能在不同的包之间使用。