• Base-Android快速开发框架(二)--数据存储之SharedPreferences


      对于App开发者,抽象来说,其实就是将数据以各种各样的方式展示在用户面前以及采集用户的数据。采集用户的数据包括用户的输入、触摸、传感器等,展示的数据通过网络来源于各业务系统,以及用户的

    输入数据。在这个过程中,数据存储显得由为重要。这也是我做为开端讲解的目的。良好的运用数据存储,不仅的能有效减少访问服务器的次数、用户的流量,还能加快App的响应速度,甚至简化应用的业务逻辑。数据

    存储这一块将介绍常用的2种数据缓存方式,Android SharedPreferences文件数据存储作为一章,sqlite 作为一章。另外我这一系列的介绍都不会有介绍基础的api,适合有一定基础的童鞋。

      简单粗暴,开始SharedPreferences。SharedPreferences是Android平台上一个轻量级的存储类,用来保存应用的一些常用配置。是以xml方式来保存,整体效率来看不是特别的高,对于常规的轻量级而言比SQLite

    要好不少,xml处理时Dalvik会通过自带底层的本地XML Parser解析。下面直接上代码。

      1 /**
      2  * SharedPreferences 数据存储工具类
      3  * 
      4  * 
      5  */
      6 public class SPUtil {
      7    private static String PreferenceName = "Constant";
      8 
      9    /**
     10     * 储存复杂的数据对象字段
     11     * 
     12     * @param key
     13     * @param t
     14     * @return
     15     */
     16    public static <T> boolean saveObjectToShare(String key, T t) {
     17       return saveObjectToShare(AppContext.getApplication(), PreferenceName,
     18             key, t);
     19    }
     20 
     21    /**
     22     * 储存复杂的数据字段对象
     23     * 
     24     * @param context
     25     * @param key
     26     * @param t
     27     * @return
     28     */
     29    public static <T> boolean saveObjectToShare(Context context, String key, T t) {
     30       return saveObjectToShare(context, PreferenceName, key, t);
     31    }
     32 
     33    /**
     34     * 
     35     * @param context
     36     * @param name
     37     * @param key
     38     * @param t
     39     * @return
     40     */
     41 
     42    public static <T> boolean saveObjectToShare(Context context, String name,
     43          String key, T t) {
     44       try {
     45          SharedPreferences sp = context.getSharedPreferences(name,
     46                Context.MODE_PRIVATE);
     47          // 存储
     48          Editor editor = sp.edit();
     49          if (t == null) {
     50             editor.putString(key, "");
     51             editor.commit();
     52             return true;
     53          }
     54          ByteArrayOutputStream toByte = new ByteArrayOutputStream();
     55          ObjectOutputStream oos;
     56 
     57          oos = new ObjectOutputStream(toByte);
     58          oos.writeObject(t);
     59          // 对byte[]进行Base64编码
     60          String payCityMapBase64 = new String(Base64.encode(
     61                toByte.toByteArray(), Base64.DEFAULT));
     62 
     63          editor.putString(key, payCityMapBase64);
     64          editor.commit();
     65          return true;
     66       } catch (IOException e) {
     67          e.printStackTrace();
     68          return false;
     69       }
     70    }
     71 
     72    /**
     73     * 得到复杂的数据对象
     74     * 
     75     * @param key
     76     * @return
     77     */
     78    public static <T> T getObjectFromShare(String key) {
     79       return getObjectFromShare(AppContext.getApplication(), PreferenceName,
     80             key);
     81    }
     82 
     83    /**
     84     * 得到复杂的数据对象
     85     * 
     86     * @param context
     87     * @param key
     88     * @return
     89     */
     90    public static <T> T getObjectFromShare(Context context, String key) {
     91       return getObjectFromShare(context, PreferenceName, key);
     92    }
     93 
     94    /**
     95     * 得到复杂的数据对象
     96     * 
     97     * @param key
     98     * @param object
     99     */
    100    @SuppressWarnings("unchecked")
    101    public static <T> T getObjectFromShare(Context context, String name,
    102          String key) {
    103       try {
    104          SharedPreferences sp = context.getSharedPreferences(name,
    105                Context.MODE_PRIVATE);
    106          String payCityMapBase64 = sp.getString(key, "");
    107          if (payCityMapBase64.length() == 0) {
    108             return null;
    109          }
    110          byte[] base64Bytes = Base64
    111                .decode(payCityMapBase64, Base64.DEFAULT);
    112          ByteArrayInputStream bais = new ByteArrayInputStream(base64Bytes);
    113          ObjectInputStream ois = new ObjectInputStream(bais);
    114          return (T) ois.readObject();
    115       } catch (Exception e) {
    116          e.printStackTrace();
    117       }
    118       return null;
    119    }
    120   
    121 
    122 /**
    123  * 返回String类型数据,默认是“”;
    124  * 
    125  * @param key
    126  * @return
    127  */
    128 public static String getString(String key) {
    129    SharedPreferences sp = AppContext.getApplication()
    130          .getSharedPreferences(PreferenceName, Context.MODE_PRIVATE);
    131    return sp.getString(key, "");
    132 }
    133 
    134 /**
    135  * 存储boolean数据类型
    136  * 
    137  * @param key
    138  * @param value
    139  */
    140 public static void saveboolean(String key, boolean value) {
    141    SharedPreferences sp = AppContext.getApplication()
    142          .getSharedPreferences(PreferenceName, Context.MODE_PRIVATE);
    143    Editor editor = sp.edit();
    144    editor.putBoolean(key, value);
    145    editor.commit();
    146 }
    147 
    148 /**
    149  * 返回boolean类型数据,默认是true;
    150  * 
    151  * @param key
    152  * @return
    153  */
    154 public static boolean getBoolean(String key) {
    155    SharedPreferences sp = AppContext.getApplication()
    156          .getSharedPreferences(PreferenceName, Context.MODE_PRIVATE);
    157    return sp.getBoolean(key, false);
    158 }
    159 
    160 /**
    161  * 存储int数据类型
    162  * 
    163  * @param key
    164  * @param value
    165  */
    166 public static void saveInt(String key, int value) {
    167    SharedPreferences sp = AppContext.getApplication()
    168          .getSharedPreferences(PreferenceName, Context.MODE_PRIVATE);
    169    Editor editor = sp.edit();
    170    editor.putInt(key, value);
    171    editor.commit();
    172 }
    173 
    174 /**
    175  * 返回int类型数据,默认是true;
    176  * 
    177  * @param key
    178  * @return
    179  */
    180 public static int getInt(String key) {
    181    SharedPreferences sp = AppContext.getApplication()
    182          .getSharedPreferences(PreferenceName, Context.MODE_PRIVATE);
    183    return sp.getInt(key, 0);
    184 }
    185 
    186 /**
    187  * 存储float数据类型
    188  * 
    189  * @param key
    190  * @param value
    191  */
    192 public static void saveFloat(String key, float value) {
    193    SharedPreferences sp = AppContext.getApplication()
    194          .getSharedPreferences(PreferenceName, Context.MODE_PRIVATE);
    195    Editor editor = sp.edit();
    196    editor.putFloat(key, value);
    197    editor.commit();
    198 }
    199 
    200 /**
    201  * 返回float类型数据,默认是true;
    202  * 
    203  * @param key
    204  * @return
    205  */
    206 public static float getFloat(String key) {
    207    SharedPreferences sp = AppContext.getApplication()
    208          .getSharedPreferences(PreferenceName, Context.MODE_PRIVATE);
    209    return sp.getFloat(key, 0);
    210 }
    211 
    212 /**
    213  * 存储long数据类型
    214  * 
    215  * @param key
    216  * @param value
    217  */
    218 public static void saveLong(String key, long value) {
    219    SharedPreferences sp = AppContext.getApplication()
    220          .getSharedPreferences(PreferenceName, Context.MODE_PRIVATE);
    221    Editor editor = sp.edit();
    222    editor.putLong(key, value);
    223    editor.commit();
    224 }
    225 
    226 /**
    227  * 返回long类型数据,默认是true;
    228  * 
    229  * @param key
    230  * @return
    231  */
    232 public static long getLong(String key) {
    233    SharedPreferences sp = AppContext.getApplication()
    234          .getSharedPreferences(PreferenceName, Context.MODE_PRIVATE);
    235    return sp.getLong(key, 0);
    236 
    237     }
    238 }
    SPUtil
      SPUtil作为一个工具类,封装了SharedPreferences的基本所有的静态写入、读取方法。包含int long String boolean 以及复杂的对象。可以快速的实现SharedPreferences的操作。具体在apps里面常用于一些系统的配置,比如常见
    的在个人设置里面会有是否允许消息推送、是否自动检测版本更新的checkbox。我试过有的项目的这些设置的存储也通过网络存储在服务器,但大部分都是存储在本地。另外有时候遇到过一些listview里面的数据集合的简单缓存以及读取,也没必
    要劳师动众的建sqlte表,直接做对象序列号存储,有时候可以省很多的事情。下一篇将开始讲解sqlite的数据存储,如果有什么建议,可以给我留言。
    
    
     
  • 相关阅读:
    回溯算法
    八皇后问题
    排序算法冒泡、选择、堆、插入、归并、快速、希尔
    pycharm专业版的合法激活方法
    Mathtype 7.4.4.516软件研究 https://blog.csdn.net/Hatim98/article/details/115186587
    nodeffi使用指南
    echart——vue封装成公共组件
    动态链接库dll(.so)的使用总结
    通过ffi在node.js中调用动态链接库(.so/.dll文件)
    NodeJS和NW通过ffi调用dll/so动态库
  • 原文地址:https://www.cnblogs.com/huangjunbin/p/4955160.html
Copyright © 2020-2023  润新知