做的项目里需要用SharedPreferences存储一些信息。所以写了个类来管理SharedPreferences。
package com.example.webhelper;
import java.util.HashMap;
import java.util.Map;
import android.content.Context;
import android.content.SharedPreferences;
public class mySharePreData {
private Context context;
// 构造函数
public mySharePreData(Context context) {
this.context = context;
}
// 向sharedPerences里保存一个值,标签为key,内容为content
public boolean saveOne(String key, String content) {
boolean flag = false;
SharedPreferences sharedPreferences = context.getSharedPreferences(
"btable", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(key, content);
flag = editor.commit();
return flag;
}
// 从sharedPerences里得到标签为key的值,若不存在则返回not exist
public String getOne(String key) {
SharedPreferences sharedPreferences = context.getSharedPreferences(
"btable", Context.MODE_PRIVATE);
String content = sharedPreferences.getString("ID", "not exist");
return content;
}
// 从sharedPerences里获取所有信息,以map形式返回,需要自行设定需要返回哪些标签
public Map getAll() {
Map map = new HashMap();
SharedPreferences sharedPreferences = context.getSharedPreferences(
"userinfo", Context.MODE_PRIVATE);
String content1 = sharedPreferences.getString("ID", "not exist");
map.put("key1", content1);
return map;
}
}
阅读(674) | 评论(0) | 转发(0) |