標籤:
一、SharedPreferences簡介 (1)SharedPreferences是Android平台上一個輕量級的儲存類,用來儲存應用的一些常用配置,比如Activity狀態,Activity暫停時,將此activity的狀態儲存到SharedPereferences中;當Activity重載,系統回調方法onSaveInstanceState時,再從SharedPreferences中將值取出。 (2)SharedPreferences提供了java常規的Long、Int、String等類型資料的儲存介面。 (3)SharedPreferences類似過去Windows系統上的ini設定檔,但是它分為多種許可權,可以全域共用訪問。 (4)SharedPreferences最終是以xml方式來儲存,整體效率來看不是特別的高,對於常規的輕量級而言比SQLite要好不少,如果真的儲存量不大可以考慮自己定義檔案格式。xml處理時Dalvik會通過內建底層的本地XML Parser解析,比如XMLpull方式,這樣對於記憶體資源佔用比較好。
二、執行個體 (1)Layout中的布局檔案main.xml
<?xml version="1.0" encoding="utf-8"?><TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TableRow > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="姓名:" android:textSize="20sp"/> <EditText android:id="@+id/name" android:layout_width="200dp" android:layout_height="wrap_content" android:text="" /> </TableRow> <TableRow > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="年齡:" android:textSize="20sp"/> <EditText android:id="@+id/age" android:layout_width="200dp" android:layout_height="wrap_content" android:text="" /> </TableRow> <Button android:id="@+id/save" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="儲存" android:textSize="20sp" /> <Button android:id="@+id/show" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="顯示" android:textSize="20sp" /> <!-- 用於顯示儲存在SharedPreferences這種的資料 --> <TextView android:id="@+id/result" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="" android:textSize="20sp"/></TableLayout>
(2)Activity中的MainActivity.java
package com.yby.sharedpreferences;import android.app.Activity;import android.content.Context;import android.content.SharedPreferences;import android.content.SharedPreferences.Editor;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;import android.widget.TextView;import android.widget.Toast;public class MainActivity extends Activity { private EditText name,age; private Button save,show; private TextView result; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); //載入布局檔案 setContentView(R.layout.main); //初始化UI控制項 name = (EditText) findViewById(R.id.name); age = (EditText) findViewById(R.id.age); save = (Button) findViewById(R.id.save); show = (Button) findViewById(R.id.show); result = (TextView) findViewById(R.id.result); //為按鈕監聽事件 save.setOnClickListener(listener); show.setOnClickListener(listener); } private OnClickListener listener = new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub //擷取SharedPreferences對象,最後會產生一個data.xml檔案
/** * SharedPreferences資料的四種操作模式 Context.MODE_PRIVATE Context.MODE_APPEND Context.MODE_WORLD_READABLE Context.MODE_WORLD_WRITEABLE Context.MODE_PRIVATE:為預設操作模式,代表該檔案是私人資料,只能被應用本身訪問,在該模式下,寫入的內容會覆蓋原檔案的內容 Context.MODE_APPEND:模式會檢查檔案是否存在,存在就往檔案追加內容,否則就建立新檔案. Context.MODE_WORLD_READABLE和Context.MODE_WORLD_WRITEABLE用來控制其他應用是否有許可權讀寫該檔案. MODE_WORLD_READABLE:表示當前檔案可以被其他應用讀取. MODE_WORLD_WRITEABLE:表示當前檔案可以被其他應用寫入 */
SharedPreferences preferences = MainActivity.this.getSharedPreferences("data", Context.MODE_PRIVATE); switch (v.getId()) { case R.id.save: //擷取Editor對象 Editor editor = preferences.edit(); //Editor是一個map集合,通過putXXX()向裡面塞值,key-value editor.putString("name", name.getText().toString()); editor.putInt("age", Integer.parseInt(age.getText().toString())); //此時必須要執行commit(),否則後面無法通過getXXX()來擷取對應的值 editor.commit(); Toast.makeText(MainActivity.this,"儲存成功" , Toast.LENGTH_SHORT).show(); break; case R.id.show: //getXXX(param1,param2) 第一個參數是存的時候的key,第二個是當在檔案中找不到對應的key時,給的一個預設的值 String name = preferences.getString("name", "null"); int age = preferences.getInt("age", 0); //由於我在上面沒有給bir這個變數,所以這個會顯示出來為bir is null String bir = preferences.getString("bir", "bir is null"); //往TextView裡面放儲存在SharedPreferences中的值 result.setText("name:"+name+",age:"+age+",bir:"+bir); break; default: break; } } };}
(3)運行項目實現效果
(4)通過SharedPreferences儲存的資料儲存在 /data/data/PACKAGE_NAME/shared_prefs目錄下,可以通過eclipse中的DDMS匯出XML檔案查看,也可以用命令的方式進行查看
<?xml version=‘1.0‘ encoding=‘utf-8‘ standalone=‘yes‘ ?><map><string name="name">ffff</string><int name="age" value="12" /></map>
通過命令的方式查看
android資料存放區之SharedPreferences