image.png
Android 開(kāi)發(fā)過(guò)程中會(huì)設(shè)計(jì)到數(shù)據(jù)的存儲(chǔ)
保證系統(tǒng)重啟后數(shù)據(jù)能保存凤壁,數(shù)據(jù)持久化,Android 提供5種存儲(chǔ)方案:
- 文件存儲(chǔ);
- SharedPreferences 存儲(chǔ);
- SQLite數(shù)據(jù)庫(kù)存儲(chǔ)舰蟆;
- Contentvider 存儲(chǔ)(用于不同app之間數(shù)據(jù)共享);
- 網(wǎng)絡(luò)存儲(chǔ)狸棍;
本篇文章主要記錄SharedPreferences 存儲(chǔ)簡(jiǎn)單使用:
1 .主要用途:
輕量級(jí)存儲(chǔ)類身害,保存 app 配置參數(shù),以 XML 格式保存數(shù)據(jù)草戈;
2. 保存 xml 文件路徑:
/data/data/< package-name >/shared_prefs/****.xml
3. 簡(jiǎn)單使用:
兒童鎖密碼保存 與 取出
//兒童鎖密碼保存到SharedPreferences 中
public static void setChildLockPassword(Context context, String value) {
SharedPreferences sp = context.getSharedPreferences(CHILD_LOCK, Context.MODE_PRIVATE);
sp.edit().putString(CHILD_LOCK_PASSWORD, value).commit();
}
//SharedPreferences 中取出兒童鎖密碼
public static String getChildLockPassword(Context context) {
SharedPreferences sp = context.getSharedPreferences(CHILD_LOCK, Context.MODE_PRIVATE);
//child_lock.xml文件中找“CHILD_LOCK_PASSWORD”字段對(duì)應(yīng)的數(shù)據(jù)塌鸯,沒(méi)有則返回為“空”
return sp.getString(CHILD_LOCK_PASSWORD, "");
}