SharePreference保存Object

转:https://www.jianshu.com/p/ae0ca6c2d926

一个要点,就是使用它储存的对象,必须 implement Serializable,否则会报错

package com.skt.itrip.common;

import android.app.Activity;
import android.content.SharedPreferences;
import android.util.Base64;

import com.skt.itrip.application.MyApplication;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

/**
 * SharedPreferences工具类,可以保存object对象
 * <p>
 * 存储时以object存储到本地,获取时返回的也是object对象,需要自己进行强制转换
 * <p>
 * 也就是说,存的人和取的人要是同一个人才知道取出来的东西到底是个啥 ^_^
 */
public class SharedPreferenceUtil {

    /**
     * writeObject 方法负责写入特定类的对象的状态,以便相应的 readObject 方法可以还原它
     * 最后,用Base64.encode将字节文件转换成Base64编码保存在String中
     *
     *  object 待加密的转换为String的对象
     *  String   加密后的String
     */
    private static String Object2String(Object object) {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        ObjectOutputStream objectOutputStream;
        try {
            objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
            objectOutputStream.writeObject(object);
            String string = new String(Base64.encode(byteArrayOutputStream.toByteArray(), Base64.DEFAULT));
            objectOutputStream.close();
            return string;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 使用Base64解密String,返回Object对象
     *
     *  objectString 待解密的String
     *  object      解密后的object
     */
    private static Object String2Object(String objectString) {
        byte[] mobileBytes = Base64.decode(objectString.getBytes(), Base64.DEFAULT);
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(mobileBytes);
        ObjectInputStream objectInputStream;
        try {
            objectInputStream = new ObjectInputStream(byteArrayInputStream);
            Object object = objectInputStream.readObject();
            objectInputStream.close();
            return object;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }

    /**
     * 使用SharedPreference保存对象
     *
     *  fileKey    储存文件的key
     *  key        储存对象的key
     *  saveObject 储存的对象
     */
    public static void save(String fileKey, String key, Object saveObject) {
        SharedPreferences sharedPreferences = MyApplication.getInstance().getApplicationContext().getSharedPreferences(fileKey, Activity.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        String string = Object2String(saveObject);
        editor.putString(key, string);
        editor.apply();
    }


    /**
     * 获取SharedPreference保存的对象
     *
     *  fileKey 储存文件的key
     *  key     储存对象的key
     *  object 返回根据key得到的对象
     */
    public static Object get(String fileKey, String key) {
        SharedPreferences sharedPreferences =  MyApplication.getInstance().getApplicationContext().getApplicationContext().getSharedPreferences(fileKey, Activity.MODE_PRIVATE);
        String string = sharedPreferences.getString(key, null);
        if (string != null) {
            return String2Object(string);
        } else {
            return null;
        }
    }
}

待保存的Object对象(实现 Serializable )

public class UserInfoEntity implements Serializable {

    public String mAppId;

    public int mPlatForm;

    public List<ClassInner> mList;

    public static class ClassInner implements Serializable{
        public int id;
        public List<ClassInnerOther> mInnerList;

    }

    public static class ClassInnerOther implements Serializable{
        public String name;
        public int value;
    }
}

使用SP工具类保存内容

UserInfoEntity userInfoEntity = new UserInfoEntity ();
SharedPreferenceUtil.save(Constant.SP_FILE_NAME,"UserInfoEntity",userInfoEntity);

使用SP工具类获取内容

userInfoEntity = (UserInfoEntity) SharedPreferenceUtil.get(Constant.SP_FILE_NAME,"UserInfoEntity");
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值