The use of shared preferences in Android data storage

Keywords: Android xml

Introduction to SharedPreferences

SharedPreferences is a data storage method of android. xml is stored in memory in the form of key value pairs. Files are stored in the / data / data / / shared ﹤ prefs directory. SharedPreferences
Initialization

getSharedPreferences(name,mode)
The parameters name and mode represent the memory space naming and access permission modes respectively
Name memory space name is to open up storage space belonging to your app in memory
mode access
Mode? Append: append mode storage
Mode? Private: private mode storage
Mode? World? Readable
Mode? World? Writeable open storage other app s can write to get editing objects

common method
SharedPreferences.Editor edit = sp.edit();
edit.clear(); / / clear data
edit.putString(); / / save string
edit.putInt(); / / save to int
edit.putLong(); / / save to long
edit.putBoolean(); / / save to boolean
edit.putFloat(); / / save float
edit.putStringSet(); / / save to set
sp.getString(); / / take string
sp.getInt(); / / int
sp.getBoolean(); / / get boolean
sp.getLong(); / / long
sp.getAll(); / / take map

My packaged sp management tool
    public class SPUtils {
    /**
     * sq Construct hold context object
     * @param context
     */
    public static SharedPreferences sp;
    public static Context context;


    public  SPUtils(Context context) {
        this.context = context;
        if (sp==null){
            sp = context.getSharedPreferences(Constant.SPKEY, Context.MODE_PRIVATE);
        }
    }


    /**
     * getString/char
      * @param key Field name
     * @return value Return value
     */
    public static String getString(String key){
        String value = sp.getString(key, "");
        if (!TextUtils.isEmpty(value)){
            return value;
        }else {
            return Constant.BACKERROR+"";
        }
    }

    /**
     * getInt Method
     * @param key Field name
     *
     * @return value int Return value
     */
    public static int getInt(String key){
        int value = sp.getInt(key, 0);
        if (value!=0){
            return value;
        }else {
            return Constant.BACKERROR;
        }
    }

    /**
     * getLong Method
     *
     * @param key Field name
     * @return Long Return value
     */
    public static Long getLong(String key){
        Long value = sp.getLong(key, 0);
        if (value!=0){
            return value;
        }else {
            return Long.valueOf(Constant.BACKERROR);
        }
    }
    /**
     *  putString Method
     *
     * @param key Field name
     * @param value field value
     */
    public static void putString(String key,String value){
        sp.edit().putString(key,value).commit();

    }
    /**
     *  putInt Method
     *
     * @param key Field name
     * @param value field value
     */
    public static void putInt(String key,int value){
        sp.edit().putInt(key,value).commit();

    }
    /**
     *  putLong Method
     *
     * @param key Field name
     * @param value field value
     */
    public static void putLong(String key,Long value){
        sp.edit().putLong(key,value).commit();

    }
        // Clear data
    public static void clearSpSpace(){
        sp.edit().clear().commit();
    }

}

Posted by Blesbok on Tue, 31 Mar 2020 10:57:06 -0700