封装的代码
package com.liuan.ok_demo;
import android.content.Context;
import android.content.SharedPreferences;
import static com.example.liuan.mytoolsquickbottom.MyApp.appContext;
public class SpUtils {
private static SharedPreferences sp;
public static void getSharedPreference(Context context) {
if (sp == null) {
sp = context.getSharedPreferences("config", context.MODE_PRIVATE);
}
}
public static void putString(Context context, String key, String value) {
getSharedPreference(context);
sp.edit().putString(key, value).commit();
}
private static Context appContext;
public static void init(Context context) {
appContext = context;
}
public static String getString(Context context, String key, String defValue) {
getSharedPreference(context);
return sp.getString(key, defValue);
}
public static void putInt(Context context, String key, int value) {
getSharedPreference(context);
sp.edit().putInt(key, value).commit();
}
public static int getInt(Context context, String key, int defValue) {
getSharedPreference(context);
return sp.getInt(key, defValue);
}
public static void putBoolean(Context context, String key, boolean value) {
getSharedPreference(context);
sp.edit().putBoolean(key, value).commit();
}
public static boolean getBoolean(Context context, String key,
boolean defValue) {
getSharedPreference(context);
return sp.getBoolean(key, defValue);
}
public static void putLong(Context context, String key, Long value) {
getSharedPreference(context);
sp.edit().putLong(key, value).commit();
}
public static Long getLong(Context context, String key,
Long defValue) {
getSharedPreference(context);
return sp.getLong(key, defValue);
}
/**
* 移除
*/
public static void remove(Context context, String key) {
getSharedPreference(context);
sp.edit().remove(key).commit();
}
//单参数构造
public static void putString(String key, String value) {
getSharedPreference(appContext);
sp.edit().putString(key, value).commit();
}
public static String getString(String key, String defValue) {
getSharedPreference(appContext);
return sp.getString(key, defValue);
}
public static void putInt(String key, int value) {
getSharedPreference(appContext);
sp.edit().putInt(key, value).commit();
}
public static int getInt(String key, int defValue) {
getSharedPreference(appContext);
return sp.getInt(key, defValue);
}
public static void putBoolean(String key, boolean value) {
getSharedPreference(appContext);
sp.edit().putBoolean(key, value).commit();
}
public static boolean getBoolean(String key,
boolean defValue) {
getSharedPreference(appContext);
return sp.getBoolean(key, defValue);
}
public static void putLong(String key, Long value) {
getSharedPreference(appContext);
sp.edit().putLong(key, value).commit();
}
public static Long getLong(String key,
Long defValue) {
getSharedPreference(appContext);
return sp.getLong(key, defValue);
}
/**
* 移除
*/
public static void remove(String key) {
getSharedPreference(appContext);
sp.edit().remove(key).commit();
}
}
三个参数
Context.MODE_PRIVATE: 指定该SharedPreferences数据只能被本应用程序读、写。
Context.MODE_WORLD_READABLE: 指定该SharedPreferences数据能被其他应用程序读,但不能写。
Context.MODE_WORLD_WRITEABLE: 指定该SharedPreferences数据能被其他应用程序读,写
原理
底层是使用键值对的方式存储在xml中
数据存储在/data/data/<package name>/shared_prefs目录下