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();
    }

 

转载于:https://www.cnblogs.com/maxiaodoubao/p/3194473.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值