Shared Preferences คือ การอ่านและเก็บข้อมูลโดยที่สามารถนำข้อมูลนั้นกลับมาใช้ใหม่ได้โดยที่เราปิด Application แล้วทำการเปิดขึ้นมาใหม่ก็ยังมีข้อมูลที่เราเก็บไปแล้วแสดงผลอยู่
่*Shared Preferences มีการเก็บข้อมูลเป็น Plaint Text*Shared Preferences เก็บข้อมูลเป็นไฟล์ xml
ประเภทของข้อมูลที่ Shared Preferences สามารถเก็บได้
String (String key, String value)
StringSet (String key, Set<String>values)
Int (String key, int value)
Boolean (String key, boolean value)
Float (String key, float value)
Long (String key, long value)
ตัวอย่างการใช้งานการเก็บข้อมูล
String s = "Shared Preferences"
SharedPreferences sPref = getSharedPreferences("sP", Context.MODE_PRIVATE);
SharedPreferences.Editor edit = sPref.edit();
edit.putString("Key",s);
edit.commit()
ตัวอย่างการอ่านข้อมูล
edit.getString("Key", "");
ตัวอย่างการลบข้อมูล
edit.remove("Key");ตัวอย่างการลบข้อมูลทั้งหมด
edit.commit();
edit.clear();
edit.commit();
ปล.ผิดพลาดประการใดขออภัยด้วยครับ
