Dhaval2404

SharedPreferenceUtils

Apr 13th, 2016
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.23 KB | None | 0 0
  1. public class SharedPreferenceUtils {
  2.     private static final String SHARED_PREFERENCE_TAG = "MyApp";
  3.  
  4.     private SharedPreferenceUtils() {
  5.         throw new AssertionError();
  6.     }
  7.  
  8.     /**
  9.      * Get SharedPreferences Object.
  10.      * <br>
  11.      * @param mContext The context to use.  Usually your {@link android.app.Application}
  12.      *                 or {@link Activity} object.
  13.      * <br>
  14.      * @return Returns the SharedPreferences object
  15.      */
  16.     public static SharedPreferences getSharedPreferences(Context mContext) {
  17.         return mContext.getSharedPreferences(SHARED_PREFERENCE_TAG, Activity.MODE_PRIVATE);
  18.     }
  19.  
  20.     /**
  21.      * Retrieve a String value from the preferences.
  22.      * <br>
  23.      * @param mContext The context to use.  Usually your {@link android.app.Application}
  24.      *                 or {@link Activity} object.
  25.      * @param key      The name of the preference.
  26.      * <br>
  27.      * @return Returns the preference value if it exists, or null
  28.      */
  29.     public static String getString(Context mContext, String key) {
  30.         return getSharedPreferences(mContext).getString(key, null);
  31.     }
  32.  
  33.     /**
  34.      * Retrieve a String value from the preferences.
  35.      * <br>
  36.      * @param mContext The context to use.  Usually your {@link android.app.Application}
  37.      *                 or {@link Activity} object.
  38.      * @param key          The name of the preference.
  39.      * @param defaultValue Value to return if this preference does not exist.
  40.      * <br>
  41.      * @return Returns the preference value if it exists, or defaultValue
  42.      */
  43.     public static String getString(Context mContext, String key, String defaultValue) {
  44.         return getSharedPreferences(mContext).getString(key, defaultValue);
  45.     }
  46.  
  47.     /**
  48.      * Set a String value in the preferences
  49.      *
  50.      * @param mContext The context to use.  Usually your {@link android.app.Application}
  51.      *                 or {@link Activity} object.
  52.      * @param key      The name of the preference.
  53.      * @param value    The value for the preference.
  54.      * <br><br>
  55.      * @return Returns true if the new values were successfully written
  56.      * to persistent storage.
  57.      */
  58.     public static boolean putString(Context mContext, String key, String value) {
  59.         SharedPreferences pref = getSharedPreferences(mContext);
  60.         SharedPreferences.Editor editor = pref.edit();
  61.         editor.putString(key, value);
  62.         return editor.commit();
  63.     }
  64.  
  65.     /**
  66.      * Retrieve a int value from the preferences.
  67.      *
  68.      * @param mContext A Context of the application.
  69.      * @param key      The name of the preference.
  70.      * <br><br>
  71.      * @return Returns the preference value if it exists, or -1
  72.      */
  73.     public static int getInt(Context mContext, String key) {
  74.         return getSharedPreferences(mContext).getInt(key, -1);
  75.     }
  76.  
  77.     /**
  78.      * Retrieve a int value from the preferences.
  79.      *
  80.      * @param mContext The context to use.  Usually your {@link android.app.Application}
  81.      *                 or {@link Activity} object.
  82.      * @param key          The name of the preference.
  83.      * @param defaultValue Value to return if this preference does not exist.
  84.      * <br><br>
  85.      * @return Returns the preference value if it exists, or defaultValue
  86.      */
  87.     public static int getInt(Context mContext, String key, int defaultValue) {
  88.         return getSharedPreferences(mContext).getInt(key, defaultValue);
  89.     }
  90.  
  91.     /**
  92.      * Set a int value in the preferences
  93.      *
  94.      * @param mContext The context to use.  Usually your {@link android.app.Application}
  95.      *                 or {@link Activity} object.
  96.      * @param key      The name of the preference.
  97.      * @param value    The value for the preference.
  98.      * <br><br>
  99.      * @return Returns true if the new values were successfully written
  100.      * to persistent storage.
  101.      */
  102.     public static boolean putInt(Context mContext, String key, int value) {
  103.         SharedPreferences pref = getSharedPreferences(mContext);
  104.         SharedPreferences.Editor editor = pref.edit();
  105.         editor.putInt(key, value);
  106.         return editor.commit();
  107.     }
  108.  
  109.     /**
  110.      * Retrieve a long value from the preferences.
  111.      *
  112.      * @param mContext The context to use.  Usually your {@link android.app.Application}
  113.      *                 or {@link Activity} object.
  114.      * @param key      The name of the preference.
  115.      * <br><br>
  116.      * @return Returns the preference value if it exists, or -1l
  117.      */
  118.     public static long getLong(Context mContext, String key) {
  119.         return getSharedPreferences(mContext).getLong(key, -1l);
  120.     }
  121.  
  122.     /**
  123.      * Retrieve a long value from the preferences.
  124.      *
  125.      * @param mContext The context to use.  Usually your {@link android.app.Application}
  126.      *                 or {@link Activity} object.
  127.      * @param key          The name of the preference.
  128.      * @param defaultValue Value to return if this preference does not exist.
  129.      * <br><br>
  130.      * @return Returns the preference value if it exists, or defaultValue
  131.      */
  132.     public static long getLong(Context mContext, String key, long defaultValue) {
  133.         return getSharedPreferences(mContext).getLong(key, defaultValue);
  134.     }
  135.  
  136.     /**
  137.      * Set a long value in the preferences
  138.      *
  139.      * @param mContext The context to use.  Usually your {@link android.app.Application}
  140.      *                 or {@link Activity} object.
  141.      * @param key      The name of the preference.
  142.      * @param value    The value for the preference.
  143.      * <br><br>
  144.      * @return Returns true if the new values were successfully written
  145.      * to persistent storage.
  146.      */
  147.     public static boolean putLong(Context mContext, String key, long value) {
  148.         SharedPreferences pref = getSharedPreferences(mContext);
  149.         SharedPreferences.Editor editor = pref.edit();
  150.         editor.putLong(key, value);
  151.         return editor.commit();
  152.     }
  153.  
  154.     /**
  155.      * Retrieve a boolean value from the preferences.
  156.      *
  157.      * @param mContext The context to use.  Usually your {@link android.app.Application}
  158.      *                 or {@link Activity} object.
  159.      * @param key      The name of the preference.
  160.      * <br><br>
  161.      * @return Returns the preference value if it exists, or false
  162.      */
  163.     public static boolean getBoolean(Context mContext, String key) {
  164.         return getSharedPreferences(mContext).getBoolean(key, false);
  165.     }
  166.  
  167.     /**
  168.      * Retrieve a boolean value from the preferences.
  169.      *
  170.      * @param mContext The context to use.  Usually your {@link android.app.Application}
  171.      *                 or {@link Activity} object.
  172.      * @param key          The name of the preference.
  173.      * @param defaultValue Value to return if this preference does not exist.
  174.      * <br><br>
  175.      * @return Returns the preference value if it exists, or defaultValue
  176.      */
  177.     public static boolean getBoolean(Context mContext, String key, boolean defaultValue) {
  178.         return getSharedPreferences(mContext).getBoolean(key, defaultValue);
  179.     }
  180.  
  181.     /**
  182.      * Set a boolean value in the preferences
  183.      *
  184.      * @param mContext The context to use.  Usually your {@link android.app.Application}
  185.      *                 or {@link Activity} object.
  186.      * @param key      The name of the preference.
  187.      * @param value    The value for the preference.
  188.      * <br><br>
  189.      * @return Returns true if the new values were successfully written
  190.      * to persistent storage.
  191.      */
  192.     public static boolean putBoolean(Context mContext, String key, boolean value) {
  193.         SharedPreferences pref = getSharedPreferences(mContext);
  194.         SharedPreferences.Editor editor = pref.edit();
  195.         editor.putBoolean(key, value);
  196.         return editor.commit();
  197.     }
  198.  
  199.  
  200.     /**
  201.      * Remove preference value.
  202.      *
  203.      * @param mContext The context to use.  Usually your {@link android.app.Application}
  204.      *                 or {@link Activity} object.
  205.      * @param key      The name of the preference to put.
  206.      * <br><br>
  207.      * @return Returns true if preference value were successfully removed from
  208.      * persistent storage.
  209.      */
  210.     public static <E>boolean putList(Context mContext, String key, List<E> objectList){
  211.         return SharedPreferenceUtils.putString(mContext, key, new Gson().toJson(objectList));
  212.     }
  213.  
  214.     /**
  215.      * Remove preference value.
  216.      *
  217.      * @param mContext The context to use.  Usually your {@link android.app.Application}
  218.      *                 or {@link Activity} object.
  219.      * @param key          The name of the preference.
  220.      * <br><br>
  221.      * @return Returns true if preference value were successfully removed from
  222.      * persistent storage.
  223.      */
  224.     public static <E>List<E> getList(Context mContext, String key){
  225.         TypeToken<List<E>> token = new TypeToken<List<E>>() {};
  226.         String json = SharedPreferenceUtils.getString(mContext, key, null);
  227.         List<E> objectList = new Gson().fromJson(json, token.getType());
  228.         return objectList==null?Collections.<E>emptyList():objectList;
  229.     }
  230.  
  231.     /**
  232.      * Remove preference value.
  233.      *
  234.      * @param mContext The context to use.  Usually your {@link android.app.Application}
  235.      *                 or {@link Activity} object.
  236.      * @param key      The name of the preference to remove.
  237.      * <br><br>
  238.      * @return Returns true if preference value were successfully removed from
  239.      * persistent storage.
  240.      */
  241.     public static boolean remove(Context mContext, String key) {
  242.         SharedPreferences pref = getSharedPreferences(mContext);
  243.         SharedPreferences.Editor editor = pref.edit();
  244.         editor.remove(key);
  245.         return editor.commit();
  246.     }
  247.  
  248.     /**
  249.      * clear all preference value.
  250.      *
  251.      * @param mContext The context to use.  Usually your {@link android.app.Application}
  252.      *                 or {@link Activity} object.
  253.      * <br><br>
  254.      * @return Returns true if all preference value successfully removed from
  255.      * persistent storage.
  256.      */
  257.     public static boolean clear(Context mContext) {
  258.         SharedPreferences pref = getSharedPreferences(mContext);
  259.         SharedPreferences.Editor editor = pref.edit();
  260.         editor.clear();
  261.         return editor.commit();
  262.     }
  263.  
  264. }
Add Comment
Please, Sign In to add comment