Android数据存储知识总结一(小水一波~~)
第一种获取共享参数的方式是使用键值对Shared Preferences 存储结构是key-value键值对,类似于xml文件
SharedPreference shared = getSharedPreference ("share",MODE_PRIVATE);//这里的share是我们起的名字(可以理解为如果没有share.xml文件的话就会新建一个share.xml文件);
往共享参数里面存储数据需要借助到Editor类,具体使用如下:
SharedPerference.Editor editor = shared.edit();//获取编辑器对象
editor.putString("name","zhangsan");
editor.putString("password","123456");
editor.commit();//直接将数据写入到磁盘
editor.apply();//先把数据写入内存,再异步写入磁盘
如何获取SharedPerference内容
SharedPreference shared = getSharedPreference ("share",MODE_PRIVATE);//先得到共享参数对象
String name = shared.getString("name"," ");//第二个参数为默认值
第二种方式为使用DataStore数据仓库,使用需要导包
其次需要写一个Util类
public class DatastoreUtil { private static DatastoreUtil instance; // 声明一个数据仓库工具的实例 private RxDataStore<Preferences> mDataStore; // 声明一个数据仓库实例 private DatastoreUtil(Context context) { mDataStore = new RxPreferenceDataStoreBuilder(context.getApplicationContext(), "datastore").build(); } // 获取数据仓库工具的实例 public static DatastoreUtil getInstance(Context context) { if (instance == null) { instance = new DatastoreUtil(context); } return instance; } // 获取指定名称的字符串值 public String getStringValue(String key) { Preferences.Key<String> keyId = PreferencesKeys.stringKey(key); Flowable<String> flow = mDataStore.data().map(prefs -> prefs.get(keyId)); try { return flow.blockingFirst(); } catch (Exception e) { return ""; } } // 设置指定名称的字符串值 public void setStringValue(String key, String value) { Preferences.Key<String> keyId = PreferencesKeys.stringKey(key); Single<Preferences> result = mDataStore.updateDataAsync(prefs -> { MutablePreferences mutablePrefs = prefs.toMutablePreferences(); //String oldValue = prefs.get(keyId); mutablePrefs.set(keyId, value); return Single.just(mutablePrefs); }); } // 获取指定名称的整型数 public Integer getIntValue(String key) { Preferences.Key<Integer> keyId = PreferencesKeys.intKey(key); Flowable<Integer> flow = mDataStore.data().map(prefs -> prefs.get(keyId)); try { return flow.blockingFirst(); } catch (Exception e) { return 0; } } // 设置指定名称的整型数 public void setIntValue(String key, Integer value) { Preferences.Key<Integer> keyId = PreferencesKeys.intKey(key); Single<Preferences> result = mDataStore.updateDataAsync(prefs -> { MutablePreferences mutablePrefs = prefs.toMutablePreferences(); //Integer oldValue = prefs.get(keyId); mutablePrefs.set(keyId, value); return Single.just(mutablePrefs); }); } // 获取指定名称的双精度数 public Double getDoubleValue(String key) { Preferences.Key<Double> keyId = PreferencesKeys.doubleKey(key); Flowable<Double> flow = mDataStore.data().map(prefs -> prefs.get(keyId)); try { return flow.blockingFirst(); } catch (Exception e) { return 0.0; } } // 设置指定名称的双精度数 public void setDoubleValue(String key, Double value) { Preferences.Key<Double> keyId = PreferencesKeys.doubleKey(key); Single<Preferences> result = mDataStore.updateDataAsync(prefs -> { MutablePreferences mutablePrefs = prefs.toMutablePreferences(); //Double oldValue = prefs.get(keyId); mutablePrefs.set(keyId, value); return Single.just(mutablePrefs); }); } // 获取指定名称的布尔值 public Boolean getBooleanValue(String key) { Preferences.Key<Boolean> keyId = PreferencesKeys.booleanKey(key); Flowable<Boolean> flow = mDataStore.data().map(prefs -> prefs.get(keyId)); try { return flow.blockingFirst(); } catch (Exception e) { return false; } } // 设置指定名称的布尔值 public void setBooleanValue(String key, Boolean value) { Preferences.Key<Boolean> keyId = PreferencesKeys.booleanKey(key); Single<Preferences> result = mDataStore.updateDataAsync(prefs -> { MutablePreferences mutablePrefs = prefs.toMutablePreferences(); //Boolean oldValue = prefs.get(keyId); mutablePrefs.set(keyId, value); return Single.just(mutablePrefs); }); } }
最后是使用:
DatastoreUtil datastore = DataStoreUtil.getInstance(this);//先获取一个单例对象
datastore.set或者getStringValue("name");