SharedPrefs工具类

SharedPrefs分装了全部的基本类型,还进行了对象的分装,对象分装一定要让对象实现Serializable让对象序列化。

package com.pa.manager.util;

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

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Base64;

import com.lidroid.xutils.util.LogUtils;
import com.pa.manager.log.Log;

/**
 * SharedPreferences封装类
 */
public class SharedPrefsUtil {
private static final String sharedName="sp";
    static SharedPreferences sp = null;

    public static SharedPreferences getSp(Context context) {
        if (sp == null) {
            sp = context.getSharedPreferences(sharedName, 0);
        }
        return sp;
    }
    public static SharedPreferences getSp( Context context,String sharedName) {
        if (TextUtils.isEmpty(sharedName)|| sp== null) {
            sp = context.getSharedPreferences(sharedName, 0);
        }
        return sp;
    }
    public static void clearallSP(Context context){
        SharedPreferences.Editor spEdit = getSp(context).edit();
        spEdit.clear();
        spEdit.commit();
    }
    public static void clearTOkeySP(Context context,String key){
        SharedPreferences.Editor spEdit = getSp(context).edit();
        spEdit.clear();
        spEdit.commit();
    }

    /**
     * 查询缓存值
     * 
     * @param context
     * @param key
     * @param id
     */
    public static void putInt(Context context, String key, int id) {
        SharedPreferences.Editor spEdit = getSp(context).edit();
        spEdit.putInt(key, id);
        spEdit.commit();
    }

    /**
     * 查询缓存值
     * 
     * @param context
     * @param key
     * @return
     */
    public static int getInt(Context context, String key) {
        return getSp(context).getInt(key, -1);
    }

    /**
     * 查询缓存值
     * 
     * @param context
     * @param key
     * @param str
     */
    public static void putString(Context context, String key, String str) {
        SharedPreferences.Editor spEdit = getSp(context).edit();
        spEdit.putString(key, str);
        spEdit.commit();
    }

    /**
     * 获取string
     */
    public static String getString(Context context, String key) {
        return getSp(context).getString(key, null);
    }

    /**
     * 获取Float
     */
    public static float getFloat(Context context, String key) {
        return getSp(context).getFloat(key, -1F);
    }

    /**
     * 设置
     */
    public static void putLong(Context context, String key, long l) {
        SharedPreferences.Editor spEdit = getSp(context).edit();
        spEdit.putLong(key, l);
        spEdit.commit();
    }

    /**
     * 
     */
    public static long getLong(Context context, String key) {
        return getSp(context).getLong(key, -1L);
    }

    public static long getLong(Context context, String key, long defaultValue) {
        return getSp(context).getLong(key, defaultValue);
    }

    /**
     * 查询缓存值
     * 
     * @param context
     * @param key
     * @param b
     */
    public static void putBoolean(Context context, String key, boolean b) {
        SharedPreferences.Editor spEdit = getSp(context).edit();
        spEdit.putBoolean(key, b);
        spEdit.commit();
    }

    /**
     * 查询缓存值
     * 
     * @param context
     * @param key
     * @return
     */
    public static boolean getBoolean(Context context, String key) {
        return getSp(context).getBoolean(key, false);
    }

    /**
     * 缓存Set集合值
     * 
     * @param context
     * @param key
     * @param ids
     */
    public static void putSet(Context context, String key, Set<String> ids) {
        SharedPreferences.Editor spEdit = getSp(context).edit();
        spEdit.putStringSet(key, ids);
        spEdit.commit();
    }

    public static void remove(Context context, String key) {
        SharedPreferences.Editor spEdit = getSp(context).edit();
        spEdit.remove(key);
        spEdit.commit();
    }

    /**
     * SharedPreferenc 存储对象
     * 
     * @param context
     * @param key
     * @param obj
     */
    public static void putObjectStream(Context context, String key, Object obj) {
        ByteArrayOutputStream baos;
        try {
            baos = new ByteArrayOutputStream();
            ObjectOutputStream oos = new ObjectOutputStream(baos);
            oos.writeObject(obj);
            // Save user preferences. use Editor object to make changes.
            SharedPreferences.Editor spEdit = getSp(context).edit();
            String strObj = new String(Base64.encodeToString(baos.toByteArray(), Base64.DEFAULT));
            spEdit.putString(key, strObj);
            spEdit.commit();
        } catch (IOException e) {
            Log.e("IOException", e.toString());
        }
    }

    /**
     * 从SharedPreferences文件流中获取对象
     * 
     * @param context
     * @param key
     * @return
     */
    public static Object getObjectStream(Context context, String key) {
        try {
            String paramBase64 = getSp(context).getString(key, null);
            byte[] base64Bytes = Base64.decode(paramBase64.getBytes(), Base64.DEFAULT);
            ByteArrayInputStream bais = new ByteArrayInputStream(base64Bytes);
            ObjectInputStream ois = new ObjectInputStream(bais);
            return ois.readObject();
        } catch (Exception e) {
            Log.d("Exception", "" + e.toString());
        }
        return null;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值