安卓开发之SharedPreferences的工具类分享(包括保存复杂对象)

我直接贴出我工具类代码,如下:

 /**
 * Created by Peng on 2017/10/25.
 */

public class SharedPreferencesUtils {

    private static final String TAG = SharedPreferencesUtils.class.getName();

    /**
     * 保存在手机本地的文件
     */
    private static final String FILE_NAME = "file_data";

    /**
     * 清空数据
     *
     * @param context 上下文
     */
    public static void clear(Context context) {
        SharedPreferences sp = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
        sp.edit().clear().commit();
    }

    /**
     * 保存boolean变量
     */
    public static void saveBoolean(Context context, String key, boolean value) {
        SharedPreferences sp = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.putBoolean(key, value).apply();
        editor.commit();
    }

    /**
     * 获取boolean变量的值
     *
     * @param context
     * @param key
     * @param defValue 获取不到时,给定的默认的值
     * @return
     */
    public static boolean getBoolean(Context context, String key, boolean defValue) {
        SharedPreferences sp = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
        return sp.getBoolean(key, defValue);
    }


    /**
     * 保存字符串变量
     */
    public static void saveString(Context context, String key, String value) {
        SharedPreferences sp = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
        sp.edit().putString(key, value).commit();
    }

    /**
     * 获取字符串的值
     *
     * @param context
     * @param key
     * @param defValue 获取不到时,给定的默认的值
     * @return
     */
    public static String getString(Context context, String key, String defValue) {
        SharedPreferences sp = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
        return sp.getString(key, defValue);
    }


    /**
     * 保存整型变量
     */
    public static void saveInt(Context context, String key, int value) {
        SharedPreferences sp = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
        sp.edit().putInt(key, value).commit();
    }

    /**
     * 获取整型变量的值
     *
     * @param context
     * @param key
     * @param defValue 获取不到时,给定的默认的值
     * @return
     */
    public static int getInt(Context context, String key, int defValue) {
        SharedPreferences sp = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
        return sp.getInt(key, defValue);
    }

    /**
     * @param context
     * @param key
     * @param value   保存的值
     */
    public static void saveLong(Context context, String key, long value) {
        SharedPreferences sp = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
        sp.edit().putLong(key, value).commit();
    }

    /**
     * @param context
     * @param key
     * @param value   获取不到后的默认值
     * @return
     */
    public static long getLong(Context context, String key, long value) {
        SharedPreferences sp = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
        return sp.getLong(key, value);
    }


    /*****************************************保存对象相关******************************************/
    /**
     * 保存序列化对象到本地(使用时记得在bean类中实现Serializable接口)
     *
     * @param context
     * @param key
     * @param object
     */
    public static void saveObject(Context context, String key, Object object) {
        SharedPreferences.Editor spEdit = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE).edit();
        //先将序列化结果写到byte缓存中,其实就分配一个内存空间
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectOutputStream os = null;
        try {
            os = new ObjectOutputStream(bos);
            os.writeObject(object);//将对象序列化写入byte缓存
        } catch (IOException e) {
            e.printStackTrace();
        }
        //将序列化的数据转为16进制保存
        String bytesToHexString = bytesToHexString(bos.toByteArray());
        //保存该16进制数组
        spEdit.putString(key, bytesToHexString);
        spEdit.commit();
    }

    /**
     * desc:将数组转为16进制
     *
     * @param bArray
     * @return
     */
    public static String bytesToHexString(byte[] bArray) {
        if (bArray == null) {
            return null;
        }
        if (bArray.length == 0) {
            return "";
        }
        StringBuffer sb = new StringBuffer(bArray.length);
        String sTemp;
        for (int i = 0; i < bArray.length; i++) {
            sTemp = Integer.toHexString(0xFF & bArray[i]);
            if (sTemp.length() < 2)
                sb.append(0);
            sb.append(sTemp.toUpperCase());
        }
        return sb.toString();
    }

    /**
     * 从本地反序列化获取对象
     *
     * @param context
     * @param key
     * @return
     */
    public static Object getObject(Context context, String key) {
        SharedPreferences sp = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
        if (sp.contains(key)) {
            String string = sp.getString(key, "");
            if (TextUtils.isEmpty(string)) {
                return null;
            } else {
                //将16进制的数据转为数组,准备反序列化
                byte[] stringToBytes = StringToBytes(string);
                ByteArrayInputStream bis = new ByteArrayInputStream(stringToBytes);
                ObjectInputStream is = null;
                //返回反序列化得到的对象
                Object readObject = null;
                try {
                    is = new ObjectInputStream(bis);
                    readObject = is.readObject();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return readObject;
            }
        }
        return null;
    }

    /**
     * desc:将16进制的数据转为数组
     *
     * @param data
     * @return
     */
    public static byte[] StringToBytes(String data) {
        String hexString = data.toUpperCase().trim();
        if (hexString.length() % 2 != 0) {
            return null;
        }
        byte[] retData = new byte[hexString.length() / 2];
        for (int i = 0; i < hexString.length(); i++) {
            int int_ch;  // 两位16进制数转化后的10进制数
            char hex_char1 = hexString.charAt(i); //两位16进制数中的第一位(高位*16)
            int int_ch1;
            if (hex_char1 >= '0' && hex_char1 <= '9')
                int_ch1 = (hex_char1 - 48) * 16;   // 0 的Ascll - 48
            else if (hex_char1 >= 'A' && hex_char1 <= 'F')
                int_ch1 = (hex_char1 - 55) * 16; // A 的Ascll - 65
            else
                return null;
            i++;
            char hex_char2 = hexString.charAt(i); //两位16进制数中的第二位(低位)
            int int_ch2;
            if (hex_char2 >= '0' && hex_char2 <= '9')
                int_ch2 = (hex_char2 - 48); // 0 的Ascll - 48
            else if (hex_char2 >= 'A' && hex_char2 <= 'F')
                int_ch2 = hex_char2 - 55; // A 的Ascll - 65
            else
                return null;
            int_ch = int_ch1 + int_ch2;
            retData[i / 2] = (byte) int_ch;//将转化后的数放入Byte里
        }
        return retData;
    }

    /*****************************************保存对象相关******************************************/

}

当然我也写了个简单的测试例子,该代码如下(下面有下载例子地址):

/**
 * 关于测试SharedPreferencesUtils工具类的界面
 */
public class SharedPreferenceActivity extends AppCompatActivity {
    /**
     * 测试保存对象
     */
    public static final String RESUMEKEY = "resumekey";
    /**
     * 测试保存的布尔值
     */
    public static final String BOOLEANKEY = "booleanKey";
    /**
     * 测试保存的字符串
     */
    public static final String STRINGKEY = "stringkey";
    /**
     * 测试保存的整型
     */
    public static final String INTKEY = "intKey";

    /**
     * 测试保存的Long型
     */
    public static final String LONGKEY = "longkey";
    /**
     * 上下文
     */
    private Context mContext;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mContext = this;
        /**
         * 关于测试SharedPreferencesUtils工具类
         */
        goTestSharedPreferencesUtils();
    }
    /**
     * 关于测试SharedPreferencesUtils工具类
     */
    private void goTestSharedPreferencesUtils() {
        /**
         *测试保存布尔值
         */
        SharedPreferencesUtils.saveBoolean(mContext, BOOLEANKEY, true);//保存为true
        Log.i("testBoolean", SharedPreferencesUtils.getBoolean(mContext, BOOLEANKEY, false) + "");//获取也为true
        /**
         *测试保存的字符串
         */
        SharedPreferencesUtils.saveString(mContext, STRINGKEY, "太阳");//保存为太阳
        Log.i("testString", SharedPreferencesUtils.getString(mContext, STRINGKEY, "") + "");//获取也为太阳
        /**
         *测试保存的整型
         */
        SharedPreferencesUtils.saveInt(mContext, INTKEY, 10);//保存为10
        Log.i("testInt", SharedPreferencesUtils.getInt(mContext, INTKEY, 0) + "");//获取也为10
        /**
         *测试保存的Long
         */
        SharedPreferencesUtils.saveLong(mContext, LONGKEY, 10000L);//保存为10000
        Log.i("testLong", SharedPreferencesUtils.getLong(mContext, LONGKEY, 0L) + "");//获取也为10000
        /**
         *测试本地保存对象
         */
        savaData(createObject());
        readData();
    }
    /**
     * 创建一个测试对象
     * @return
     */
    private Resume createObject() {
        Resume resume = new Resume();
        resume.setId(1);
        resume.setName("小鹏");
        resume.setCity("广州");
        Resume.MyFood myFood = new Resume.MyFood();
        myFood.setType("水果");
        ArrayList<Resume.MyFood.FoodListBean> myFoods = new ArrayList<>();
        Resume.MyFood.FoodListBean foodListBean = new Resume.MyFood.FoodListBean();
        foodListBean.setFoodImg("图片");
        foodListBean.setFoodName("苹果");
        myFood.setFoodListBeen(myFoods);
        resume.setMyFood(myFood);
        return resume;
    }
    /**
     * 保存测试对象
     *
     * @param resume
     */
    private void savaData(Resume resume) {
        SharedPreferencesUtils.saveObject(mContext, RESUMEKEY, resume);
    }
    /**
     * 读取保存后的测试对象
     */
    private void readData() {
        Resume resume = (Resume) SharedPreferencesUtils.getObject(mContext, RESUMEKEY);
        Log.i("resume", resume.getId() + "/" + resume.getCity() + "/" + resume.getMyFood().getType());
    }
}

需要下载这个例子的可以去github下载:
下载地址https://github.com/pengAndroid/android_my_utils.git

猜你喜欢

转载自blog.csdn.net/qq_33373648/article/details/78344438