偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

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

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

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

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

如果要讀取配置文件信息,只需要直接使用SharedPreferences對象的getXXX()方法即可,而如果要寫入配置信息,則必須先調用SharedPreferences對象的edit()方法,使其處于可編輯狀態(tài),然后再調用putXXX()方法寫入配置信息,最后調用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. //寫入配置文件。可以使用SharedPreferences.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 多任務多線程斷點下載

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

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

責任編輯:zhaolei 來源: 網(wǎng)絡轉載
相關推薦

2023-11-26 09:06:46

2022-12-29 08:57:34

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

2017-01-10 19:21:06

Android APISharedPrefe

2015-07-09 13:47:37

IOSFMDB

2011-03-08 09:58:21

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

2014-08-26 10:51:44

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

2013-06-14 15:43:46

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

2010-01-26 14:43:53

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

2018-03-20 09:36:57

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

2013-03-27 09:47:01

Android開發(fā)SQAndroid SDK

2018-11-22 10:40:40

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

2014-08-26 10:04:51

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

2018-10-08 13:52:28

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

2018-06-07 16:33:31

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

2025-04-17 08:23:55

DataStore本地存儲

2018-07-13 09:20:30

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

2022-06-01 07:33:29

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

2018-05-25 09:31:00

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

2019-08-27 15:00:09

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

2011-04-28 09:36:22

海量數(shù)據(jù)存儲
點贊
收藏

51CTO技術棧公眾號