zoukankan      html  css  js  c++  java
  • android菜鸟学习笔记18----Android数据存储(二)SharedPreferences

    数据存储的方式,有比直接文件读写更加简便的方式,那就是操作SharedPreferences。

    SharedPreferences一般用于存储用户的偏好设定,暂时不支持多进程操作。

    SharedPreferences是一个接口,可以通过Context和Activity中提供的方法获得SharedPreferences的实例。SharedPreferences的数据存放在/data/data/应用包名/shared_pref/目录下。

    获取方法:

    1.Context.getSharedPreferences(String name, int mode);name用于指定数据存储的文件名,mode同文件读写中的模式。

    2.Activity.getPreferences(int mode):返回一个私有的仅能被当前Activity访问的SharedPreferences实例,自动将类名作为文件名。

    3.PreferenceManager. getDefaultSharedPreferences(Context context):传入context参数,返回该context上下文所对应的包名作为前缀来命名的文件。

    SharedPreferences的使用:

    写数据:

    通过SharedPreferences.Editor这个内部类的对象来向SharedPreferences对象添加或修改数据。SharedPreferences的edit()方法可以获取这个Editor对象。Editor的putXXX()方法可以添加或修改数据。

    添加或修改的数据,还需要通过Editor的commit()方法提交之后才会真正保存到文件中。

    读数据:

    要读取SharedPreferences中的数据,直接调用它的getXXX()方法即可。

    例如,修改上一个示例中的FileUtils,添加writeSp()和readSp()方法:

     1     public static boolean writeSp(Context context, String data){
     2 
     3            try {
     4 
     5                  SharedPreferences preferences = context.getSharedPreferences("userInfo", Context.MODE_PRIVATE);
     6 
     7                  Editor editor = preferences.edit();
     8 
     9                  if(!data.contains("#")){
    10 
    11                       return false;
    12 
    13                  }
    14 
    15                  String[] strings = data.split("#");
    16 
    17                  editor.putString("username", strings[0]);
    18 
    19                  editor.putString("password", strings[1]);
    20 
    21                  editor.commit();
    22 
    23                  return true;
    24 
    25            } catch (Exception e) {
    26 
    27                  // TODO Auto-generated catch block
    28 
    29                  e.printStackTrace();
    30 
    31                  return false;
    32 
    33            }
    34 
    35       }
    36 
    37       public static String readSp(Context context){
    38 
    39            try {
    40 
    41                  SharedPreferences preferences = context.getSharedPreferences("userInfo", Context.MODE_PRIVATE);
    42 
    43                  String username = preferences.getString("username", "");
    44 
    45                  String password = preferences.getString("password", "");
    46 
    47                  return username+"#"+password;
    48 
    49            } catch (Exception e) {
    50 
    51                  // TODO Auto-generated catch block
    52 
    53                  e.printStackTrace();
    54 
    55            }
    56 
    57            return null;
    58 
    59       }

     修改MainActivity的onCreate(),对应读写文件改成调用读写SharedPreferences方法即可。

    运行结果:

     

    导出这个xml文件:

    1 <?xml version='1.0' encoding='utf-8' standalone='yes' ?>
    2 
    3 <map>
    4 
    5 <string name="password">abcdef</string>
    6 
    7 <string name="username">dqrcsc</string>
    8 
    9 </map>

     修改writeSp()和readSp()方法,改为通过Activity的getPreferences()获取SharedPreferences对象:

    1 SharedPreferences preferences = ((Activity)context).getPreferences(Context.MODE_PRIVATE);

    运行结果:

     

    可知,通过Activity获取SharedPreferences对象,默认以当前Activity的全限定名作为文件名。

    修改writeSp()和readSp()方法,改为通过PreferenceManager的getDefaultSharedPreferences(Context context)获取SharedPreferences对象:

    1 SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);

    运行结果:

     

    可知,这种方式下数据的存放文件以传入的Context对应的包名作为前缀,然后连上_preferences.xml作为文件名。

  • 相关阅读:
    内存 : CL设置
    联通积分兑换的Q币怎么兑换到QQ上
    DB2数据库表追加字段
    显示菜单项与按钮项的关联关系
    如何将Windows8系统的磁盘格式(GPT格式)转换成Windows 7系统的磁盘格式(MBR格式)
    索尼(SONY) SVE1512S7C 把WIN8降成WIN7图文教程
    SqlServer之数据库三大范式
    Python并发编程-Redis
    Python并发编程-Memcached (分布式内存对象缓存系统)
    Python并发编程-RabbitMQ消息队列
  • 原文地址:https://www.cnblogs.com/dqrcsc/p/4632189.html
Copyright © 2011-2022 走看看