用途:存储轻量数据
注意:此方式是内部存储,存在手机内存里,内部存储属于应用本身,若此应用被卸载,则存储的数据都将消失。
//1. 获取SharedPreferences实例
SharedPreferences sharedPreferences = getSharedPreferences("user_info", Context.MODE_PRIVATE);
//2. 获取操作SharedPreferences实例的编辑器Editor
SharedPreferences.Editor editor = sharedPreferences.edit();
//3. 添加数据
editor.putString("name", "xing");
editor.putBoolean("isGirl", false);
editor.putInt("age", 18);
editor.putLong("id", 2019122616530201234L);
editor.putFloat("money", 9999.99f);
//4. 提交
editor.apply();//or edit.commit();
//获取值
String name = sharedPreferences.getString("name", "no name");
getSharedPreferences(String var1, int var2);
var1: 存储的文件名
var2:操作文件的模式
//Context.MODE_PRIVATE:默认私有模式,代表该文件是私有数据,只能被应用本身访问,该模式下,写入内容会覆盖原文件的内容
//Context.MODE_APPEND:附加模式,会检查文件是否存在,存在就往文件追加内容,否则就创建新文件。
//Context.MODE_WORLD_READABLE: 公有权限模式:其他应用是否有权限读该文件, 当前文件可被其他应用读取。
//Context.MODE_WORLD_WRITEABLE: 公有权限模式:其他应用是否有权限写该文件, 当前文件可被其他应用写入。
putXXX(key, value)
//同Map的键值对模式
getString(String var1, String var2)
var1: 存储是的key,
var2:获取到的默认值
文件存储位置:
存储里面的/data/data/<应用包名>/shared_prefs
commit() 与 apply()区别?
API25中,apply方法的注释。大致意思就是apply是一个原子请求(不需要担心多线程同步问题)。
commit将同步的把数据写入磁盘和内存缓存。
apply会把数据同步写入内存缓存,然后异步保存到磁盘,可能会执行失败,失败不会收到错误回调。
如果你忽略了commit的返回值,那么可以使用apply替换任何commit的实例。
简单说就是除非你需要关心xml是否写入文件成功,否则你应该在所有调用commit的地方改用apply。
SharedPreferences是个单例,所以任意Context拿到的都是同一个实例。
SharedPreferences在实例化的时候会把SharedPreferences对应的xml文件内容全部读取到内存。
对于非多进程兼容的SharedPreferences的读操作是从内存读取的,不涉及IO操作。
写入的时候由于内存已经保存了完整的xml数据,然后新写入的数据也会同步更新到内存,所以无论是用commit还是apply都不会影响立即读取。
SharedPreferences本身已经做了内存缓存。
public class SPTools {
public static final String name_file_sp = "name_file_sp";
public static void setString(Context context,String key,String value){
SharedPreferences sp = context.getSharedPreferences(name_file_sp, Context.MODE_PRIVATE);
sp.edit().putString(key, value).apply();
}
public static String getString(Context context,String key,String value){
SharedPreferences sp = context.getSharedPreferences(name_file_sp, Context.MODE_PRIVATE);
return sp.getString(key, value);
}
public static void setBoolean(Context context,String key,boolean value){
SharedPreferences sp = context.getSharedPreferences(name_file_sp,Context.MODE_PRIVATE);
sp.edit().putBoolean(key,value).apply();
}
public static boolean getBoolean(Context context,String key,boolean value){
SharedPreferences sp = context.getSharedPreferences(name_file_sp,Context.MODE_PRIVATE);
return sp.getBoolean(key,value);
}
public static void setInt(Context context,String key,int value){
SharedPreferences sp = context.getSharedPreferences(name_file_sp, Context.MODE_PRIVATE);
sp.edit().putInt(key, value).apply();
}
public static int getInt(Context context, String key, int value){
SharedPreferences sp = context.getSharedPreferences(name_file_sp, Context.MODE_PRIVATE);
return sp.getInt(key, value);
}
public static void setLong(Context context,String key,Long value){
SharedPreferences sp = context.getSharedPreferences(name_file_sp, Context.MODE_PRIVATE);
sp.edit().putLong(key, value).apply();
}
public static Long getLong(Context context,String key,Long value){
SharedPreferences sp = context.getSharedPreferences(name_file_sp, Context.MODE_PRIVATE);
return sp.getLong(key, value);
}
public static String InputToString(Context context,String key){
String result = null;
try {
InputStream is = context.getAssets().open(key);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while((len = is.read(buffer))!=-1){
baos.write(buffer, 0, len);
}
is.close();
result = baos.toString();
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
}