android 之 SharedPreferences 保存对象数据

通常我们认为Sharedpreferences能够保存简单的数据类型 int double float等等,其实它也能保存一个对象,不过需要先将对象进行Base64编码后才能保存到Sharedpreferences中,我封装了一个类

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import org.apache.commons.codec.binary.Base64;

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

public class SharedPreferencesUtil {
    private static Context mContext;
    private static SharedPreferencesUtil sp = new SharedPreferencesUtil();
    private static SharedPreferences saveInfo;
    private static Editor saveEditor;

    public static SharedPreferencesUtil getInstance(Context context){

        mContext =context;
        if (saveInfo == null && mContext != null) {
            saveInfo = mContext.getSharedPreferences("News",Context.MODE_PRIVATE);
            saveEditor = saveInfo.edit();
        }
        return sp;
    }
    private SharedPreferencesUtil() {
        if (mContext != null) {
            saveInfo = mContext.getSharedPreferences("News",Context.MODE_PRIVATE);
            saveEditor = saveInfo.edit();

        }
    }

    /**
     * @param key
     * @param value
     */
    public void saveSerializable(String key, Object value) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try {
            ObjectOutputStream oos = new ObjectOutputStream(baos);
            oos.writeObject(value);
            oos.flush();
            if (oos != null)
                oos.close();
            String strData = new String(Base64.encodeBase64(baos.toByteArray()));
            saveEditor.putString(key, strData);
            saveEditor.commit();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (baos != null)
                    baos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * @param key
     * @return
     */
    public Object restoreSerializable(String key) {
        Object obj = null;
        String objectInfo = saveInfo.getString(key, null);
        if (objectInfo == null) {
            return null;
        }
        ByteArrayInputStream bais = new ByteArrayInputStream(Base64.decodeBase64(objectInfo.getBytes()));
        try {
            ObjectInputStream ois = new ObjectInputStream(bais);
            obj = ois.readObject();
            ois.close();
        } catch (StreamCorruptedException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            try {
                if (bais != null)
                    bais.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return obj;
    }

通过单例模式产生一个Utils工具,可以通过Utils对需要保存的对象进行保存,获取保存的对象等等操作

阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/baidu_29835301/article/details/49902681
个人分类: android
上一篇android 之 如何打开只知道应用名称的另一个应用
下一篇android 之 JNI 调用 NDK
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭