Android SharedPreferences儲存,sharedpreferences
一 概念
SharedPreferences儲存方式是Android中儲存輕量級資料的一種方式。SharedPreferences儲存主要用來儲存一些簡單的配置資訊,內部以Map方式進行儲存,因此需要使用索引值對提交和儲存資料,儲存的資料以xml格式存放在本地的/data/data/<package name>/shares_prefs檔案夾下。
二 特點
1, 使用簡單,便於儲存輕量級的資料;
2, 只支援Java基礎資料型別 (Elementary Data Type),不支援自訂資料類型;
3, SharedPreferences是單例對象,在整個應用內資料共用,無法在其他應用內共用資料;
三 注意事項
1, SharedPreferences建立的時候使用的檔案名稱不同,得到的對象不同,在儲存位置會建立多個xml檔案,不同檔案名稱的SharedPreferences的資料不會共用;建立時採用相同的檔案名稱,得到多個SharedPreferences引用,此時這多個引用共用同一個xml檔案,它們操作的資料為相同的資料;
2, 針對資料的增刪改查操作只有在提交後操作才會生效,此步驟最容易被忽略,當執行了對資料的操作後得不到需要的效果時,請查看是否沒有提交操作。
3, 三種建立模式中最好採用MODE_PRIVATE,使用其他兩種模式建立容易引起安全問題,就算採用MODE_PRIVATE,當別人活得root許可權後也可能泄露使用者的重要訊息,因此建議使用SharedPreferences時,如果要儲存使用者名稱和密碼時,不要明文儲存,應該使用加密儲存,防止重要隱私泄露,引起損失。
四 使用
1, 獲得SharedPreferences對象
SharedPreferences對象必須使用上下文獲得,使用時注意先要獲得上下文。獲得SharedPreferences對象方法為:
SharedPreferences sharedPreferences = getSharedPreferences(參數一, 參數二);
參數一為要儲存的xml檔案名稱,不同的檔案名稱產生的對象不同,但同一檔案名稱可以產生多個引用,從而可以保證資料共用。此處注意指定參數一時,不用加xml尾碼,由系統自動添加。
參數二為建立模式,共有三個值:
MODE_PRIVATE
MODE_WORLD_READABLE
MODE_WORLD_WRITEABLE
第一個值使得SharedPreferences儲存的資料只能在本應用內獲得,第二個和第三個值分別使得其他應用可以讀和讀寫本應用SharedPreferences儲存的資料。由此可能帶來安全問題,請參考本文二(3)部分。
2, 獲得editor對象
使用以上獲得的SharedPreferences對象產生editor,方法為:
Editor editor = sharedPreferences.edit();
3, 對資料實現增刪改查
添加資料使用以下方法:
editor.putString(key, value);
可以實現資料的更新只需添加同鍵的索引值對,和操作Map集合一樣。
刪除資料:
editor.remove(key);
刪除參數部分鍵的索引值對。
查詢資料:
String result = sharedPreferences.getString(key1, key2);
Key1是要查詢的鍵,返回對應的值,當鍵不存在時,返回key2作為結果。
清空資料
editor.clear();
注意:無論是否同一個sharedPreferences對象,若是產生多個editor,不同的editor之間對資料的操作不會相互影響,此處容易犯錯誤,例如,以下的程式:
sharedPreferences.edit().putString(key1, value1);
sharedPreferences.edit().putString(key2, value2);
sharedPreferences.edit().putString(key3, value3);
sharedPreferences.edit().commit();
執行後這種方式無法儲存,因為sp.edit()每次都會返回一個新的Editor對象,Editor的實作類別EditorImpl裡面會有一個緩衝的Map,最後commit的時候先將緩衝裡面的Map寫入記憶體中的Map,然後將記憶體中的Map寫進XML檔案中。使用上面的方式commit,由於sp.edit()又重新返回了一個新的Editor對象,緩衝中的Map是空的,所以導致資料無法被儲存。這裡即需要注意,只有提交時,不同的editor無法完成既定的任務。而增加,刪除,更新,查詢,若是同一個sharedPreferences產生的editor,則對共用的資料有效,會按照既定的順序工作。而不同sharedPreferences產生的editor則因為處理的xml檔案不同而不能共用資料。
再次強調:所有使用editor操作的資料,必須經過同一個editor提交即commit後才會生效。
五 Android執行個體
以下使用Android執行個體完成以上內容的驗證:
1, Activity介面和功能
(以下描述控制項的順序是布局中的由上到下)
儲存資料功能:在第一個EditText中寫入要儲存的鍵,在第二個EditText中寫入要儲存對應鍵的值,點擊第一個按鈕則儲存資料;
查詢資料功能:在第三個EditText中寫入要查詢的鍵,點擊第二個按鈕查詢資料,查詢出的值顯示在第一個TextView中;
刪除資料功能:在第四個EditText中寫入要刪除的鍵,點擊第三個按鈕則刪除鍵和值;
清空資料功能:點擊第四個按鈕則清空與editor對應的xml檔案中的所有資料,但不會影響其他xml檔案。
2, 主要代碼
①MainActivity.java
package com.example.sharedpreferences;import android.os.Bundle;import android.app.Activity;import android.content.SharedPreferences;import android.content.SharedPreferences.Editor;import android.text.Editable;import android.view.Menu;import android.view.View;import android.widget.EditText;import android.widget.TextView;public class MainActivity extends Activity { private EditText et_key; private EditText et_value; private EditText et_query; private SharedPreferences sharedPreferences; private TextView tv_query; private EditText et_delete; private Editor editor; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); sharedPreferences = getSharedPreferences("info2", MODE_PRIVATE); editor = sharedPreferences.edit(); et_key = (EditText) findViewById(R.id.et_key); et_value = (EditText) findViewById(R.id.et_value); et_query = (EditText) findViewById(R.id.et_query); tv_query = (TextView) findViewById(R.id.tv_content); et_delete = (EditText) findViewById(R.id.et_delete); } public void insert(View v) { //擷取索引值資料 String key = et_key.getText().toString().trim(); String value = et_value.getText().toString().trim(); //使用editor儲存資料 editor.putString(key, value); //注意一定要提交資料,此步驟容易忘記 editor.commit(); } public void query(View v) { //獲得查詢的鍵 String query_text = et_query.getText().toString().trim(); //使用SharedPreferences查詢資料 String result = sharedPreferences.getString(query_text, null); if(result == null) { tv_query.setText("您查詢的資料不存在"); }else { tv_query.setText(result); } } public void delete(View v) { //獲得刪除的鍵 String delete = et_delete.getText().toString().trim(); //使用editor刪除資料 editor.remove(delete); //一定要提交,該步驟非常容易忽略 editor.commit(); } public void clear(View v) { //使用editor清空資料 editor.clear(); //一定要提交,該步驟非常容易忽略 editor.commit(); } }
②activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:orientation="vertical" tools:context=".MainActivity" > <EditText android:id="@+id/et_key" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="@string/text_key" /> <EditText android:id="@+id/et_value" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="@string/text_value" /> <Button android:id="@+id/bt_insert" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="right" android:onClick="insert" android:text="@string/text_insert" /> <EditText android:id="@+id/et_query" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="@string/text_et_value" /> <Button android:id="@+id/bt_query" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="right" android:onClick="query" android:layout_marginLeft="88dp" android:text="@string/text_bt_query" /> <TextView android:id="@+id/tv_content" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="@string/text_tv_query" android:textSize="20sp" /> <Button android:id="@+id/bt_delete" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/text_bt_delete" android:layout_gravity="right" android:onClick="delete" /> <EditText android:id="@+id/et_delete" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="@string/text_et_delete" /> <Button android:id="@+id/bt_clear" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/text_bt_clear" android:layout_gravity="right" android:onClick="clear" /> </LinearLayout>