SharedPreference 一个公共类

  1. import android.content.Context;  
  2. import android.content.SharedPreferences;  
  3.   
  4. /** 
  5.  * PreferencesUtils, easy to get or put data 
  6.  * <ul> 
  7.  * <strong>Preference Name</strong> 
  8.  * <li>you can change preference name by {@link #PREFERENCE_NAME}</li> 
  9.  * </ul> 
  10.  * <ul> 
  11.  * <strong>Put Value</strong> 
  12.  * <li>put string {@link #putString(Context, String, String)}</li> 
  13.  * <li>put int {@link #putInt(Context, String, int)}</li> 
  14.  * <li>put long {@link #putLong(Context, String, long)}</li> 
  15.  * <li>put float {@link #putFloat(Context, String, float)}</li> 
  16.  * <li>put boolean {@link #putBoolean(Context, String, boolean)}</li> 
  17.  * </ul> 
  18.  * <ul> 
  19.  * <strong>Get Value</strong> 
  20.  * <li>get string {@link #getString(Context, String)}, {@link #getString(Context, String, String)}</li> 
  21.  * <li>get int {@link #getInt(Context, String)}, {@link #getInt(Context, String, int)}</li> 
  22.  * <li>get long {@link #getLong(Context, String)}, {@link #getLong(Context, String, long)}</li> 
  23.  * <li>get float {@link #getFloat(Context, String)}, {@link #getFloat(Context, String, float)}</li> 
  24.  * <li>get boolean {@link #getBoolean(Context, String)}, {@link #getBoolean(Context, String, boolean)}</li> 
  25.  * </ul> 
  26.  *  
  27.  * @author <a href="http://www.trinea.cn" target="_blank">Trinea</a> 2013-3-6 
  28.  */  
  29. public class PreferencesUtils {  
  30.   
  31.     public static String PREFERENCE_NAME = "ChadAndroidCommon";  
  32.   
  33.     /** 
  34.      * put string preferences 
  35.      *  
  36.      * @param context 
  37.      * @param key The name of the preference to modify 
  38.      * @param value The new value for the preference 
  39.      * @return True if the new values were successfully written to persistent storage. 
  40.      */  
  41.     public static boolean putString(Context context, String key, String value) {  
  42.         SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);  
  43.         SharedPreferences.Editor editor = settings.edit();  
  44.         editor.putString(key, value);  
  45.         return editor.commit();  
  46.     }  
  47.   
  48.     /** 
  49.      * get string preferences 
  50.      *  
  51.      * @param context 
  52.      * @param key The name of the preference to retrieve 
  53.      * @return The preference value if it exists, or null. Throws ClassCastException if there is a preference with this 
  54.      *         name that is not a string 
  55.      * @see #getString(Context, String, String) 
  56.      */  
  57.     public static String getString(Context context, String key) {  
  58.         return getString(context, key, null);  
  59.     }  
  60.   
  61.     /** 
  62.      * get string preferences 
  63.      *  
  64.      * @param context 
  65.      * @param key The name of the preference to retrieve 
  66.      * @param defaultValue Value to return if this preference does not exist 
  67.      * @return The preference value if it exists, or defValue. Throws ClassCastException if there is a preference with 
  68.      *         this name that is not a string 
  69.      */  
  70.     public static String getString(Context context, String key, String defaultValue) {  
  71.         SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);  
  72.         return settings.getString(key, defaultValue);  
  73.     }  
  74.   
  75.     /** 
  76.      * put int preferences 
  77.      *  
  78.      * @param context 
  79.      * @param key The name of the preference to modify 
  80.      * @param value The new value for the preference 
  81.      * @return True if the new values were successfully written to persistent storage. 
  82.      */  
  83.     public static boolean putInt(Context context, String key, int value) {  
  84.         SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);  
  85.         SharedPreferences.Editor editor = settings.edit();  
  86.         editor.putInt(key, value);  
  87.         return editor.commit();  
  88.     }  
  89.   
  90.     /** 
  91.      * get int preferences 
  92.      *  
  93.      * @param context 
  94.      * @param key The name of the preference to retrieve 
  95.      * @return The preference value if it exists, or -1. Throws ClassCastException if there is a preference with this 
  96.      *         name that is not a int 
  97.      * @see #getInt(Context, String, int) 
  98.      */  
  99.     public static int getInt(Context context, String key) {  
  100.         return getInt(context, key, -1);  
  101.     }  
  102.   
  103.     /** 
  104.      * get int preferences 
  105.      *  
  106.      * @param context 
  107.      * @param key The name of the preference to retrieve 
  108.      * @param defaultValue Value to return if this preference does not exist 
  109.      * @return The preference value if it exists, or defValue. Throws ClassCastException if there is a preference with 
  110.      *         this name that is not a int 
  111.      */  
  112.     public static int getInt(Context context, String key, int defaultValue) {  
  113.         SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);  
  114.         return settings.getInt(key, defaultValue);  
  115.     }  
  116.   
  117.     /** 
  118.      * put long preferences 
  119.      *  
  120.      * @param context 
  121.      * @param key The name of the preference to modify 
  122.      * @param value The new value for the preference 
  123.      * @return True if the new values were successfully written to persistent storage. 
  124.      */  
  125.     public static boolean putLong(Context context, String key, long value) {  
  126.         SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);  
  127.         SharedPreferences.Editor editor = settings.edit();  
  128.         editor.putLong(key, value);  
  129.         return editor.commit();  
  130.     }  
  131.   
  132.     /** 
  133.      * get long preferences 
  134.      *  
  135.      * @param context 
  136.      * @param key The name of the preference to retrieve 
  137.      * @return The preference value if it exists, or -1. Throws ClassCastException if there is a preference with this 
  138.      *         name that is not a long 
  139.      * @see #getLong(Context, String, long) 
  140.      */  
  141.     public static long getLong(Context context, String key) {  
  142.         return getLong(context, key, -1);  
  143.     }  
  144.   
  145.     /** 
  146.      * get long preferences 
  147.      *  
  148.      * @param context 
  149.      * @param key The name of the preference to retrieve 
  150.      * @param defaultValue Value to return if this preference does not exist 
  151.      * @return The preference value if it exists, or defValue. Throws ClassCastException if there is a preference with 
  152.      *         this name that is not a long 
  153.      */  
  154.     public static long getLong(Context context, String key, long defaultValue) {  
  155.         SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);  
  156.         return settings.getLong(key, defaultValue);  
  157.     }  
  158.   
  159.     /** 
  160.      * put float preferences 
  161.      *  
  162.      * @param context 
  163.      * @param key The name of the preference to modify 
  164.      * @param value The new value for the preference 
  165.      * @return True if the new values were successfully written to persistent storage. 
  166.      */  
  167.     public static boolean putFloat(Context context, String key, float value) {  
  168.         SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);  
  169.         SharedPreferences.Editor editor = settings.edit();  
  170.         editor.putFloat(key, value);  
  171.         return editor.commit();  
  172.     }  
  173.   
  174.     /** 
  175.      * get float preferences 
  176.      *  
  177.      * @param context 
  178.      * @param key The name of the preference to retrieve 
  179.      * @return The preference value if it exists, or -1. Throws ClassCastException if there is a preference with this 
  180.      *         name that is not a float 
  181.      * @see #getFloat(Context, String, float) 
  182.      */  
  183.     public static float getFloat(Context context, String key) {  
  184.         return getFloat(context, key, -1);  
  185.     }  
  186.   
  187.     /** 
  188.      * get float preferences 
  189.      *  
  190.      * @param context 
  191.      * @param key The name of the preference to retrieve 
  192.      * @param defaultValue Value to return if this preference does not exist 
  193.      * @return The preference value if it exists, or defValue. Throws ClassCastException if there is a preference with 
  194.      *         this name that is not a float 
  195.      */  
  196.     public static float getFloat(Context context, String key, float defaultValue) {  
  197.         SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);  
  198.         return settings.getFloat(key, defaultValue);  
  199.     }  
  200.   
  201.     /** 
  202.      * put boolean preferences 
  203.      *  
  204.      * @param context 
  205.      * @param key The name of the preference to modify 
  206.      * @param value The new value for the preference 
  207.      * @return True if the new values were successfully written to persistent storage. 
  208.      */  
  209.     public static boolean putBoolean(Context context, String key, boolean value) {  
  210.         SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);  
  211.         SharedPreferences.Editor editor = settings.edit();  
  212.         editor.putBoolean(key, value);  
  213.         return editor.commit();  
  214.     }  
  215.   
  216.     /** 
  217.      * get boolean preferences, default is false 
  218.      *  
  219.      * @param context 
  220.      * @param key The name of the preference to retrieve 
  221.      * @return The preference value if it exists, or false. Throws ClassCastException if there is a preference with this 
  222.      *         name that is not a boolean 
  223.      * @see #getBoolean(Context, String, boolean) 
  224.      */  
  225.     public static boolean getBoolean(Context context, String key) {  
  226.         return getBoolean(context, key, false);  
  227.     }  
  228.   
  229.     /** 
  230.      * get boolean preferences 
  231.      *  
  232.      * @param context 
  233.      * @param key The name of the preference to retrieve 
  234.      * @param defaultValue Value to return if this preference does not exist 
  235.      * @return The preference value if it exists, or defValue. Throws ClassCastException if there is a preference with 
  236.      *         this name that is not a boolean 
  237.      */  
  238.     public static boolean getBoolean(Context context, String key, boolean defaultValue) {  
  239.         SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);  
  240.         return settings.getBoolean(key, defaultValue);  
  241.     }  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值