//Sharedpreferences类,通过键值对的方式,用来存取数据
public class MySharedpreferences {
private Context context;
private SharedPreferences sharedPreferences;
//构造方法
public MySharedpreferences(Context context) {
this.context = context;
sharedPreferences = context.getSharedPreferences("mySPF",
Context.MODE_PRIVATE);
}
//保存数据
public boolean saveData(String key, String value) {
boolean flag = false;
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(key, value);
flag = editor.commit();
return flag;
}
//取数据
public String getData(String key) {
String result = sharedPreferences.getString(key, "");
return result;
}
}
//在Activity里
private MySharedpreferences mySharedpreferences;
mySharedpreferences = new MySharedpreferences(context);
//保存数据
mySharedpreferences.saveData("color", Color.BLUE + "");
//取出数据
String result = mySharedpreferences.getData("color");