有一种场景,比如App设置页中会有一组开关选项,这个时候保存这些开关的状态,如果每个按钮都对应一个boolean值的话,太大材小用显得鸡肋,频繁读取SharedPreferences 存取效率自然快不过一次读取。
首先,敲定每个boolean值存储的位置1
2
3
4
5private int mBroadcastCustomValue = 0; //用来存储的int值
public static final int BROADCAST_TYPE_CUSTOM_BASE = 1;
public static final int BROADCAST_TYPE_CUSTOM_TRAFFIC = 1 << 1;
public static final int BROADCAST_TYPE_CUSTOM_CAMERA = 1 << 2;
public static final int BROADCAST_TYPE_CUSTOM_SAFE = 1 << 3;
一、添加Add
1 | private void addLevel(int level){ |
二、删除Delete
1 | private void deleteLevel(int level){ |
三、读取Read
1 | /** |