SharedPreferences是一种轻型的数据存储方式,基于xml文件存储Key-Value键值对数据。只支持存储一些基本数据类型,如string, boolean, float, int型数据。通常用来存储程序中的一些简单的配置信息。
下面给出一个SharedPreferences操作的工具类:
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
public class SharePreferencesUtil {
public static boolean save(Context context, String fileName, String key, String value) {
SharedPreferences preferences = context.getSharedPreferences(fileName, Context.MODE_PRIVATE);
Editor editor = preferences.edit();
editor.putString(key, value);
return editor.commit();
}
public static boolean save(Context context, String fileName, String key, int value) {
SharedPreferences preferences = context.getSharedPreferences(fileName, Context.MODE_PRIVATE);
Editor editor = preferences.edit();
editor.putInt(key, value);
return editor.commit();
}
public static boolean save(Context context, String fileName, String key, float value) {
SharedPreferences preferences = context.getSharedPreferences(fileName, Context.MODE_PRIVATE);
Editor editor = preferences.edit();
editor.putFloat(key, value);
return editor.commit();
}
public static boolean save(Context context, String fileName, String key, long value) {
SharedPreferences preferences = context.getSharedPreferences(fileName, Context.MODE_PRIVATE);
Editor editor = preferences.edit();
editor.putLong(key, value);
return editor.commit();
}
public static boolean save(Context context, String fileName, String key, boolean value) {
SharedPreferences preferences = context.getSharedPreferences(fileName, Context.MODE_PRIVATE);
Editor editor = preferences.edit();
editor.putBoolean(key, value);
return editor.commit();
}
public static boolean remove(Context context, String fileName, String key) {
SharedPreferences preferences = context.getSharedPreferences(fileName,
Context.MODE_PRIVATE);
Editor editor = preferences.edit();
editor.remove(key);
return editor.commit();
}
public static String getString(Context context, String fileName, String key) {
SharedPreferences preferences = context.getSharedPreferences(fileName,
Context.MODE_PRIVATE);
return preferences.getString(key, null);
}
public static int getInt(Context context, String fileName, String key) {
SharedPreferences preferences = context.getSharedPreferences(fileName,
Context.MODE_PRIVATE);
return preferences.getInt(key, 0);
}
public static boolean getBoolean(Context context, String fileName, String key) {
SharedPreferences preferences = context.getSharedPreferences(fileName,
Context.MODE_PRIVATE);
return preferences.getBoolean(key, false);
}
public static long getLong(Context context, String fileName, String key) {
SharedPreferences preferences = context.getSharedPreferences(fileName,
Context.MODE_PRIVATE);
return preferences.getLong(key, 0);
}
public static float getFloat(Context context, String fileName, String key) {
SharedPreferences preferences = context.getSharedPreferences(fileName,
Context.MODE_PRIVATE);
return preferences.getFloat(key, 0);
}
}
//保存数据
1、save(Context context, String fileName, String key, long value)
参数:
context: 上下文环境
fileName: 保存的文件名称
key: 需要保存的键
value: 键对应的值
save是个重载方法,可以对不同类型数据的报存。
//获取String类型的键值
getString(Context context, String fileName, String key)
//获取Int类型的键值
getInt(Context context, String fileName, String key)
//获取Float类型的键值
getFloat(Context context, String fileName, String key)
//获取Long类型的键值
getLong(Context context, String fileName, String key)
//获取Boolean类型的键值
getBoolean(Context context, String fileName, String key)
参数:
context:上下文环境
fileName: 文件名称
key: 需要获取的键
//删除键值
remove(Context context, String fileName, String key)
参数:
context:上下文环境
fileName: 文件名称
key: 需要获取的键