Data save type management

package com.woqi.caigou.utils;

 

import android.content.Context;

import android.content.SharedPreferences;

import android.content.SharedPreferences.Editor;

import android.text.TextUtils;

import android.util.Base64;

 

 

import com.google.gson.Gson;

 

import java.io.ByteArrayInputStream;

import java.io.ByteArrayOutputStream;

import java.io.IOException;

import java.io.ObjectInputStream;

import java.io.ObjectOutputStream;

import java.io.StreamCorruptedException;

import java.util.ArrayList;

import java.util.List;

 

import org.json.JSONArray;

import org.json.JSONException;

 

/**

 * --------------------------------------------

 * Description:

 * 1.SharedPreferences tool class

 * Encapsulates the basic operations on Sp data storage

 * -------------------------------------------

 */

public class SpUtils {

    private final static String SP_NAME = "share_data";

    private static SharedPreferences sp;

 

    private static SharedPreferences getSp(Context context) {

        if (sp == null) {

            sp = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);

        }

        return sp;

    }

 

    /**

     * Get boolean data

     *

     * @param context

     * @param key

     * @return if there is no value, return false

     */

    public static boolean getBoolean(Context context, String key) {

        SharedPreferences sp = getSp(context);

        return sp.getBoolean(key, false);

    }

 

    /**

     * Get boolean data

     *

     * @param context

     * @param key

     * @param defValue

     * @return

     */

    public static boolean getBoolean(Context context, String key, boolean defValue) {

        SharedPreferences sp = getSp(context);

        return sp.getBoolean(key, defValue);

    }

 

    /**

     * save boolean cache

     *

     * @param context

     * @param key

     * @param value

     */

    public static void setBoolean(Context context, String key, boolean value) {

        SharedPreferences sp = getSp(context);

        Editor editor = sp.edit();

        editor.putBoolean(key, value);

        editor.commit();

    }

 

    /**

     * Get String data

     *

     * @param context

     * @param key

     * @return if there is no value, return null

     */

    public static String getString(Context context, String key) {

        SharedPreferences sp = getSp(context);

        return sp.getString(key, null);

    }

 

    /**

     * Get String data

     *

     * @param context

     * @param key

     * @param defValue

     * @return

     */

    public static String getString(Context context, String key, String defValue) {

        SharedPreferences sp = getSp(context);

        return sp.getString(key, defValue);

    }

 

    /**

     * Save String cache

     *

     * @param context

     * @param key

     * @param value

     */

    public static void setString(Context context, String key, String value) {

        SharedPreferences sp = getSp(context);

        Editor editor = sp.edit();

        editor.putString(key, value);

        editor.commit();

    }

 

    /**

     * Get int data

     *

     * @param context

     * @param key

     * @return if no value, return -1

     */

    public static int getInt(Context context, String key) {

        SharedPreferences sp = getSp(context);

        return sp.getInt(key, -1);

    }

 

    /**

     * Get int data

     *

     * @param context

     * @param key

     * @param defValue

     * @return

     */

    public static int getInt(Context context, String key, int defValue) {

        SharedPreferences sp = getSp(context);

        return sp.getInt(key, defValue);

    }

 

    /**

     * save int cache

     *

     * @param context

     * @param key

     * @param value

     */

    public static void setInt(Context context, String key, int value) {

        SharedPreferences sp = getSp(context);

        Editor editor = sp.edit();

        editor.putInt(key, value);

        editor.commit();

    }

 

 

    /**

     * Get int data

     *

     * @param context

     * @param key

     * @return if no value, return -1

     */

    public static long getLong(Context context, String key) {

        SharedPreferences sp = getSp(context);

        return sp.getLong(key, -1);

    }

 

    /**

     * Get int data

     *

     * @param context

     * @param key

     * @param defValue

     * @return

     */

    public static long getLong(Context context, String key, long defValue) {

        SharedPreferences sp = getSp(context);

        return sp.getLong(key, defValue);

    }

 

    /**

     * save int cache

     *

     * @param context

     * @param key

     * @param value

     */

    public static void setLong(Context context, String key, long value) {

        SharedPreferences sp = getSp(context);

        Editor editor = sp.edit();

        editor.putLong(key, value);

        editor.commit();

    }

 

    public static float getFloat(Context context, String key) {

        SharedPreferences sp = getSp(context);

        return sp.getFloat(key, 0);

    }

 

    /**

     * Get int data

     *

     * @param context

     * @param key

     * @param defValue

     * @return

     */

    public static float getFloat(Context context, String key, float defValue) {

        SharedPreferences sp = getSp(context);

        return sp.getFloat(key, defValue);

    }

 

    /**

     * save int cache

     *

     * @param context

     * @param key

     * @param value

     */

    public static void setFloat(Context context, String key, float value) {

        SharedPreferences sp = getSp(context);

        Editor editor = sp.edit();

        editor.putFloat(key, value);

        editor.commit();

    }

 

    /**

     * save object

     *

     * @param context

     * @param object saved object

     */

    public static void setObject(Context context, Object object, String key) throws Exception{

        String strJson = "";

        Gson gson = new Gson ();

        strJson = gson.toJson (object);

        SpUtils.setString(context, key,strJson);

    }

 

    /**

     * get object

     *

     * @param context

     * @param key

     * @return

     */

    public static Object getObject(Context context, String key, Class clazz) throws Exception{

        Object obj = null;

        String str = getSp(context).getString(key, "");

        if (!TextUtils.isEmpty(str)) {

            Gson gson = new Gson ();

            obj = (Object) gson.fromJson (str, clazz);

        }

        return obj;

    }

 

 

    /**

     * Save the List object collection

     * @param context

     * @param key

     * @param dates

     */

    public static void setListObj(Context context, String key, List<?> datas) {

        JSONArray mJsonArray = new JSONArray();

for (int i = 0; i < datas.size(); i++) {

Object bean = datas.get(i);

mJsonArray.put(bean);

}

Editor editor = getSp(context).edit();

editor.putString(key, mJsonArray.toString());

editor.commit();

}

 

 

    /**

     * Get local List persistent data

     *

     * @paramcontext

     * @paramkey

     * @return

     */

    public static List<?> getListObj(Context context , String key ){

List<Object> list = new ArrayList<>();

String result = getSp(context).getString(key, "");

try {

JSONArray jsonArray = new JSONArray(result);

for(int i = 0; i <jsonArray.length(); i++){

Object bean = jsonArray.get(i);

list.add(bean);

}

} catch (JSONException e) {

e.printStackTrace ();

 

}

return list;

}

    public static String setListString(List<Object> list)

            throws IOException {

        // Instantiate a ByteArrayOutputStream object to load the compressed byte file.

        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

        // Then load the resulting character data into ObjectOutputStream

        ObjectOutputStream objectOutputStream = new ObjectOutputStream(

                byteArrayOutputStream);

        // The writeObject method is responsible for writing the state of an object of a particular class so that the corresponding readObject method can restore it

        objectOutputStream.writeObject(list);

        // Finally, use Base64.encode to convert the byte file to Base64 encoding and save it in String

        String SceneListString = new String(Base64.encode(

                byteArrayOutputStream.toByteArray(), Base64.DEFAULT));

        // close objectOutputStream

        objectOutputStream.close();

        return SceneListString;

    }

 

    @SuppressWarnings("unchecked")

    public static List<Object> getListString(String string) throws StreamCorruptedException, IOException,

            ClassNotFoundException {

        byte[] mobileBytes = Base64.decode(string.getBytes(),Base64.DEFAULT);

        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(mobileBytes);

        ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream);

        List SceneList = (List) objectInputStream.readObject();

        objectInputStream.close();

        return SceneList;

    }

 

    /**

     * Save list data

     * @param context

     * @param list

     * @param key

     */

    public static void setListObj(Context context , List list , String key){

        Editor editor = getSp(context).edit();

        try {

            String str = setListString(list);

            editor.putString(key , str);

            editor.commit();

        } catch (IOException e) {

            e.printStackTrace ();

        }

    }

 

 

 

 

 

    /**

     * Remove the value corresponding to a key

     *

     * @param context

     * @param key

     */

    public static void remove(Context context, String key) {

        Editor editor = getSp(context).edit();

        editor.remove(key);

        editor.commit();

    }

 

    /**

     * Remove all data in Sp file

     *

     * @param context

     */

    public static void clear(Context context) {

        Editor editor = getSp(context).edit();

        editor.clear();

        editor.commit();

    }

}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326242533&siteId=291194637