天天看點

【轉】Android下使用配置檔案(Preferences)

<a href="http://www.aslibra.com/blog/post/android_SharedPreferences.php">http://www.aslibra.com/blog/post/android_SharedPreferences.php</a>

android下可以友善的使用key-value的配置檔案:

// S.PRE 配置檔案的檔案名

// S.PRE_STOP 配置檔案裡的key

//寫入

SharedPreferences sp = getSharedPreferences(S.PRE, 0);

SharedPreferences.Editor editor = sp.edit();

//賦予false

editor.putBoolean(S.PRE_STOP, false);

editor.commit();

//讀取

//需要指定預設值,如果配置檔案沒有或者裡面沒有這個key時的值

sp.getBoolean(S.PRE_STOP, false);

程式共享的資料可以簡單的使用此種方式讀寫,比較的友善。

檔案在哪裡?

/data/data/程式名稱/shared_prefs/{S.PRE}.xml

檔案内容:

&lt;?xml version='1.0' encoding='utf-8' standalone='yes' ?&gt;

&lt;map&gt;

&lt;boolean name="STOP" value="true" /&gt;

&lt;/map&gt;

繼續閱讀