下面通過SimplePreferenceDemo示例介紹具體說明SharedPreferences的文件保存位置和保存格式及其讀取。
圖7-1是SimplePreferenceDemo示例的用戶界面。

圖7-1 SimplePreferenceDemo示例的用戶界面
在圖7-1所示的界面中,用戶在界面上輸入信息,然后退出這個應用,即關閉Activity時將SharedPreferences進行保存。當在應用程序列表中找到這個應用重新開啟時,保存在SharedPreferences的信息(剛剛輸入的信息)將被讀取出來,并重新呈現(xiàn)在用戶界面上。
界面代碼如代碼清單7-7所示。
代碼清單7-7 main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://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>
主要實現(xiàn)代碼如代碼清單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;其次調(diào)用edit()方法使其處于可以編輯狀態(tài),并使用putString將兩個EditText中的值保存起來;后使用commit()方法提交即可保存。
SharedPreferences保存到哪里去了?
SharedPreferences是以XML的格式以文件的方式自動保存的,在SimplePreferenceDemo示例運行后,DDMS中的File Explorer展開到/data/data,可以看到Android為每個應用程序建立了與包同名的目錄,用來保存應用程序產(chǎn)生的數(shù)據(jù),這些數(shù)據(jù)包括文件、SharedPreferences文件和數(shù)據(jù)庫等;將目錄展開到/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只能在同一個包內(nèi)使用,不能在不同的包之間使用。