对于保存:
public static void saveSharedPreferencesLogList(Context context, List callLog) {
SharedPreferences mPrefs = context.getSharedPreferences(Constant.CALL_HISTORY_RC, context.MODE_PRIVATE);
SharedPreferences.Editor prefsEditor = mPrefs.edit();
Gson gson = new Gson();
String json = gson.toJson(callLog);
prefsEditor.putString("myJson", json);
prefsEditor.commit();
}
对于负载:
public static List loadSharedPreferencesLogList(Context context) {
List callLog = new ArrayList();
SharedPreferences mPrefs = context.getSharedPreferences(Constant.CALL_HISTORY_RC, context.MODE_PRIVATE);
Gson gson = new Gson();
String json = mPrefs.getString("myJson", "");
if (json.isEmpty()) {
callLog = new ArrayList();
} else {
Type type = new TypeToken>() {
}.getType();
callLog = gson.fromJson(json, type);
}
return callLog;
}
PhoneCallLog是我的自定义对象的名称。(包含字符串,长整数和布尔值)