1.概述。SharePreferences是用來儲存一些簡單配置資訊的一種機制,使用Map資料結構來儲存資料,以索引值對的方式儲存,採用了XML格式將資料存放區到裝置中。例如儲存登入使用者的使用者名稱和密碼。只能在同一個包內使用,不能在不同的包之間使用,其實也就是說只能在建立它的應用中使用,其他應用無法使用。
建立的隱藏檔儲存在/data/data/<package name>/shares_prefs檔案夾下。
2.使用。
通過Context.getSharedPreferences方法擷取SharedPreferences對象,參數分別為儲存的檔案名稱和儲存模式。
view plaincopy to clipboardprint?
- // 擷取SharedPreferences對象
- SharedPreferences sp = getSharedPreferences(DATABASE, Activity.MODE_PRIVATE);
- // 擷取Editor對象
- Editor editor = sp.edit();
3.操作。SharePreferences儲存資料是通過擷取Editor編輯器對象來操作的。
插入資料:
調用Editor.putxxxx方法,兩個參數分別為鍵和值。
擷取資料:
調用Editor.getxxxx方法,兩個參數分別為鍵和不存在指定鍵時的預設值。
刪除資料:
調用Editor.remove方法,參數為指定的鍵。
清空所有資料:
調用Editor.clear方法
上述所有方法調用都要執行Editor.commit方法來提交。
下面通過對資料的增刪改查來示範下SharePreferences的使用。
完整程式:android_sharedpreferences.rar
view plaincopy to clipboardprint?
- /**
- * MainActivity
- *
- * @author zuolongsnail
- */
- public class MainActivity extends Activity {
- private EditText keyET;
- private EditText valueET;
- private Button insertBtn;
- private Button deleteBtn;
- private Button modifyBtn;
- private Button queryBtn;
- private Button clearBtn;
- private TextView textView;
- /** 儲存的檔案名稱 */
- public static final String DATABASE = "Database";
- /** 儲存後的檔案路徑:/data/data/<package name>/shares_prefs + 檔案名稱.xml */
- public static final String PATH = "/data/data/code.sharedpreferences/shared_prefs/Database.xml";
-
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- keyET = (EditText) findViewById(R.id.key);
- valueET = (EditText) findViewById(R.id.value);
- insertBtn = (Button) findViewById(R.id.insert);
- deleteBtn = (Button) findViewById(R.id.delete);
- modifyBtn = (Button) findViewById(R.id.modify);
- queryBtn = (Button) findViewById(R.id.query);
- clearBtn = (Button) findViewById(R.id.clear);
- // 用於顯示隱藏檔中資料
- textView = (TextView) findViewById(R.id.content);
- insertBtn.setOnClickListener(new OperateOnClickListener());
- deleteBtn.setOnClickListener(new OperateOnClickListener());
- modifyBtn.setOnClickListener(new OperateOnClickListener());
- queryBtn.setOnClickListener(new OperateOnClickListener());
- clearBtn.setOnClickListener(new OperateOnClickListener());
- }
-
- class OperateOnClickListener implements OnClickListener {
- @Override
- public void onClick(View v) {
- // 擷取SharedPreferences對象
- SharedPreferences sp = getSharedPreferences(DATABASE,
- Activity.MODE_PRIVATE);
- // 擷取Editor對象
- Editor editor = sp.edit();
- // 擷取介面中的資訊
- String key = keyET.getText().toString();
- String value = valueET.getText().toString();
- switch (v.getId()) {
- // 插入資料
- case R.id.insert:
- editor.putString(key, value);
- editor.commit();
- textView.setText(MainActivity.this.print());
- break;
- // 刪除資料
- case R.id.delete:
- editor.remove(key);
- editor.commit();
- textView.setText(MainActivity.this.print());
- break;
- // 修改資料
- case R.id.modify:
- editor.putString(key, value);
- editor.commit();
- textView.setText(MainActivity.this.print());
- break;
- // 查詢資料
- case R.id.query:
- String result = sp.getString(key, "");
- textView.setText("key=" + key + ",value=" + result);
- break;
- // 清空所有資料
- case R.id.clear:
- editor.clear();
- editor.commit();
- textView.setText(MainActivity.this.print());
- break;
- }
-
- }
- }
-
- /** 擷取隱藏檔的資料 */
- private String print() {
- StringBuffer buff = new StringBuffer();
- try {
- BufferedReader reader = new BufferedReader(new InputStreamReader(
- new FileInputStream(PATH)));
- String str;
- while ((str = reader.readLine()) != null) {
- buff.append(str + "/n");
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- return buff.toString();
- }
- }
程式:
下面提供一個SharedPreferences工具類,在開發中直接調用即可。
view plaincopy to clipboardprint?
- /**
- * SharedPreferences儲存資料方式工具類
- * @author zuolongsnail
- */
- public class SharedPrefsUtil {
- public final static String SETTING = "Setting";
- public static void putValue(Context context,String key, int value) {
- Editor sp = context.getSharedPreferences(SETTING, Context.MODE_PRIVATE).edit();
- sp.putInt(key, value);
- sp.commit();
- }
- public static int getValue(Context context,String key, int defValue) {
- SharedPreferences sp = context.getSharedPreferences(SETTING, Context.MODE_PRIVATE);
- int value = sp.getInt(key, defValue);
- return value;
- }
- public static boolean getValue(Context context,String key, boolean defValue) {
- SharedPreferences sp = context.getSharedPreferences(SETTING, Context.MODE_PRIVATE);
- boolean value = sp.getBoolean(key, defValue);
- return value;
- }
- public static String getValue(Context context,String key, String defValue) {
- SharedPreferences sp = context.getSharedPreferences(SETTING, Context.MODE_PRIVATE);
- String value = sp.getString(key, defValue);
- return value;
- }
- }