自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Android數(shù)據(jù)存儲之SharedPreferences

移動開發(fā) Android
Android SharedPreferences多用于保存軟件配置參數(shù),其背后是用xml文件存放數(shù)據(jù)。本文講述了數(shù)據(jù)的存儲。

SharedPreferences是以鍵值對來存儲應(yīng)用程序的配置信息的一種方式,它只能存儲基本數(shù)據(jù)類型。一個(gè)程序的配置文件僅可以在本應(yīng)用程序中使用,或者說只能在同一個(gè)包內(nèi)使用,不能在不同的包之間使用。 實(shí)際上SharedPreferences是采用了XML格式將數(shù)據(jù)存儲到設(shè)備中,在DDMS中的File Explorer中的/data/data/<package name>/shares_prefs下。

以下表格為獲取SharedPreferences對象的兩個(gè)方法:

如果要讀取配置文件信息,只需要直接使用SharedPreferences對象的getXXX()方法即可,而如果要寫入配置信息,則必須先調(diào)用SharedPreferences對象的edit()方法,使其處于可編輯狀態(tài),然后再調(diào)用putXXX()方法寫入配置信息,最后調(diào)用commit()方法提交更改后的配置文件。

以下是示例代碼: 
 

  1. import android.app.Activity;   
  2. import android.content.SharedPreferences;   
  3. public class Calc extends Activity {   
  4. public static final String PREFS_NAME = "MyPrefsFile";   
  5. . . .   
  6. @Override   
  7. protected void onCreate(Bundle state){   
  8. super.onCreate(state);   
  9. . . .   
  10. //載入配置文件   
  11. SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);   
  12. //或者使用 SharedPreferences settings = getPreferences(0);   
  13. boolean silent = settings.getBoolean("silentMode", false);   
  14. setSilent(silent);   
  15. }   
  16. @Override   
  17. protected void onStop(){   
  18. super.onStop();   
  19. //寫入配置文件??梢允褂肧haredPreferences.Editor來輔助解決。   
  20. SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);   
  21. SharedPreferences.Editor editor = settings.edit();   
  22. editor.putBoolean("silentMode", mSilentMode);   
  23. editor.commit(); //一定要記得提交   
  24. //或者再簡單化一可以這樣寫   
  25. SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);   
  26. setting.edit().putBoolean(“silentMode”,mSilentMode).commit();   
  27. }   

【編輯推薦】

Android中preference的使用

Android 多任務(wù)多線程斷點(diǎn)下載

Android應(yīng)用程序開發(fā)環(huán)境的搭建

在Android應(yīng)用程序中使用Internet數(shù)據(jù)

責(zé)任編輯:zhaolei 來源: 網(wǎng)絡(luò)轉(zhuǎn)載
相關(guān)推薦

2023-11-26 09:06:46

2022-12-29 08:57:34

Android本地?cái)?shù)據(jù)存儲

2017-01-10 19:21:06

Android APISharedPrefe

2011-03-08 09:58:21

海量數(shù)據(jù)

2015-07-09 13:47:37

IOSFMDB

2018-03-20 09:36:57

數(shù)據(jù)倉庫數(shù)據(jù)存儲知識

2025-04-17 08:23:55

DataStore本地存儲

2014-08-26 10:04:51

數(shù)據(jù)存儲

2018-10-08 13:52:28

Android數(shù)據(jù)安全存儲安全

2013-03-27 09:47:01

Android開發(fā)SQAndroid SDK

2018-06-07 16:33:31

大數(shù)據(jù)冷熱數(shù)據(jù)存儲平臺

2010-01-26 14:43:53

Android數(shù)據(jù)存儲

2013-06-14 15:43:46

Android開發(fā)移動開發(fā)數(shù)據(jù)存儲

2014-08-26 10:51:44

數(shù)據(jù)存儲

2022-06-01 07:33:29

數(shù)據(jù)存儲加密

2018-11-22 10:40:40

存儲備份數(shù)據(jù)

2019-08-27 15:00:09

MySQL數(shù)據(jù)庫存儲

2018-07-13 09:20:30

SQLite數(shù)據(jù)庫存儲

2018-05-25 09:31:00

數(shù)據(jù)存儲高可用

2019-06-14 14:49:15

RAID數(shù)據(jù)存儲
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號