zoukankan      html  css  js  c++  java
  • SharedPreferences 用法




    private
    void getUserInfoFromPref(){ /* * 保存到文件的方法 * * Constant.user = (User)Constant.readObjectFromFile(Constant.USER_SAVE_PATH); */ //获取到sharepreference 对象, 参数一为xml文件名,参数为文件的可操作模式 SharedPreferences preferences = this.getSharedPreferences( Constant.PREF_FILE_NAME, MODE_APPEND); //获取到编辑对象 String UserId = preferences.getString("UserId",""); String UserName = preferences.getString("UserName",""); String UserPho = preferences.getString("UserPho",""); String UserQRCode = preferences.getString("UserQRCode",""); String PhotoPath = preferences.getString("PhotoPath",""); boolean hasLogined = preferences.getBoolean("hasLogined",false); Constant.user = new User(UserId,UserName,UserPho,UserQRCode,PhotoPath,hasLogined); }


        
        
       

    //保存数据 到sharePreference文件
        private void setUserInfoToPref(){
    /*        
     * 文件的方法
     *
     * File file = new File("/sdcard/myImage/");
     
            if(!file.exists()){
                  file.mkdirs();// 创建文件夹
             }
            Constant.writeObjectToFile(Constant.USER_SAVE_PATH,Constant.user);
            Log.e("user ---",Constant.readObjectFromFile(Constant.USER_SAVE_PATH).toString());
     *         
    */
            
           SharedPreferences preferences = getSharedPreferences(
                   Constant.PREF_FILE_NAME, MODE_PRIVATE);
           SharedPreferences.Editor editor = preferences.edit();
           
           if(Constant.user != null){
               editor.putString("UserId",Constant.user.UserId);
               editor.putString("UserName",Constant.user.UserName);
               editor.putString("UserPho",Constant.user.UserPho);
               editor.putString("UserQRCode",Constant.user.UserQRCode);
               editor.putString("PhotoPath",Constant.user.PhotoPath);
               editor.putBoolean("hasLogined",Constant.user.hasLogined);
           }
           
           editor.commit();
        }
  • 相关阅读:
    编程风格与注意事项(一)
    BB-Black 初体验之远程控制篇
    我的BB-Black之旅……(一)
    等精度频率计设计中的猫腻
    Linux查看物理CPU个数、核数、逻辑CPU个数、Cpu型号
    基于TransportClient的elasticsearch(es)消费kafka数据---Java程序设计
    elasticsearch概述
    HBase 性能优化笔记
    hive1.2.1搭建遇到的问题
    Hbase的表设计
  • 原文地址:https://www.cnblogs.com/maxiaodoubao/p/3194473.html
Copyright © 2011-2022 走看看