SharedPreferences tool class for Android, sharedpreferences

Source: Internet
Author: User

SharedPreferences tool class for Android, sharedpreferences

This tool is permanently maintained and updated. If you find any bug or error, please leave a message and the blogger will immediately correct it.


This tool provides the following functions:

1. Five types of data are stored;

2. read five types of data;


The content is as follows:

Import android. content. context; import android. content. sharedPreferences; import android. content. sharedPreferences. editor; /***** @ author bear ** SharedPrefereces tool class **/public class SharedPrefsUtil {/*** write int type data to SharedPreferences ** @ param context Environment * @ param name of the xml file * @ param key * @ param value */public static void putValue (Context context, string name, String key, int value) {Editor sp = getEditor (context, name); sp. putInt (key, value); sp. commit ();} /*** write boolean data to SharedPreferences ** @ param context * @ param name corresponding to the xml file name * @ param key * @ param value */ public static void putValue (Context context, string name, String key, boolean value) {Editor sp = getEditor (context, name); sp. putBoolean (key, value); sp. commit ();} /*** write String-type data to SharedPreferences ** @ param context * @ param name corresponds to the xml file name * @ param key * @ param value */ public static void putValue (Context context, string name, String key, String value) {Editor sp = getEditor (context, name); sp. putString (key, value); sp. commit ();} /*** write float data to SharedPreferences ** @ param context * @ param name corresponding to the xml file name * @ param key * @ param value */ public static void putValue (Context context, string name, String key, float value) {Editor sp = getEditor (context, name); sp. putFloat (key, value); sp. commit ();} /*** write long-type data to SharedPreferences ** @ param context * @ param name corresponds to the xml file name * @ param key * @ param value */ public static void putValue (Context context, string name, String key, long value) {Editor sp = getEditor (context, name); sp. putLong (key, value); sp. commit ();} /*** read int-type data from SharedPreferences ** @ param context * @ param name corresponds to the xml file name * @ param key * @ param defValue if no data is read if the call succeeds, the default value * @ return is used to return the read value */public static int getValue (Context context, string name, String key, int defValue) {SharedPreferences sp = getSharedPreferences (context, name); int value = sp. getInt (key, defValue); return value ;} /*** read boolean data from SharedPreferences ** @ param context * @ param name corresponds to the xml file name * @ param key * @ param defValue if no data is read if the call succeeds, the default value * @ return is used to return the read value */public static boolean getValue (Context context, string name, String key, boolean defValue) {SharedPreferences sp = getSharedPreferences (context, name); boolean value = sp. getBoolean (key, defValue); return value ;} /*** read String-type data from SharedPreferences ** @ param context * @ param name corresponds to the xml file name * @ param key * @ param defValue if no data is read if the call succeeds, the default value * @ return is used to return the read value */public static String getValue (Context context, string name, String key, String defValue) {SharedPreferences sp = getSharedPreferences (context, name); String value = sp. getString (key, defValue); return value ;} /*** read float data from SharedPreferences ** @ param context Environment * @ param name corresponding to xml file name * @ param key * @ param defValue if no data is read if the call succeeds, the default value * @ return is used to return the read value */public static float getValue (Context context, string name, String key, float defValue) {SharedPreferences sp = getSharedPreferences (context, name); float value = sp. getFloat (key, defValue); return value ;} /*** read data of the long type from SharedPreferences ** @ param context * @ param name corresponds to the xml file name * @ param key * @ param defValue if no data is read if the call succeeds, the default value * @ return is used to return the read value */public static long getValue (Context context, string name, String key, long defValue) {SharedPreferences sp = getSharedPreferences (context, name); long value = sp. getLong (key, defValue); return value;} // obtain the private static Editor getEditor (Context context, String name) {return getSharedPreferences (context, name) of the Editor instance ). edit () ;}// get the SharedPreferences instance private static SharedPreferences getSharedPreferences (Context context, String name) {return context. getSharedPreferences (name, Context. MODE_PRIVATE );}}

If your project supports version information Android API Level> = 11, you can also add the Set <String> type of storage and read.


Examples of calling methods in Activity or other places are as follows:

// Store data, and store the key-Value Pair ("color", "red") SharedPrefsUtil in the MySetting file. putValue (this, "MySetting", "color", "red ");
// Read data. Read the String color = SharedPrefsUtil. getValue (this, "MySetting", "color", "blue") with the key "color" from the MySetting file ");


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.