Android tool class ------- & gt; SharedPreferences, Android tool class

Source: Internet
Author: User

Android tool class -------> SharedPreferences, Android tool class

Import java. lang. reflect. invocationTargetException; import java. lang. reflect. method; import java. util. map; import android. content. context; import android. content. sharedPreferences; public class SPUtils {public SPUtils () {/* cannot be instantiated */throw new UnsupportedOperationException ("cannot be instantiated ");} /*** name of the file saved in the mobile phone */public static final String FILE_NAME = "pai_data";/*** to save the data, we need to get Save the specific data type, and then call Different Storage Methods ** @ param context * @ param key * @ param object */public static void put (Context context, String key, object object) {SharedPreferences sp = context. getSharedPreferences (FILE_NAME, Context. MODE_PRIVATE); SharedPreferences. editor editor = sp. edit (); if (object instanceof String) {editor. putString (key, (String) object);} else if (object instanceof Integer) {editor. putInt (key, (In Teger) object);} else if (object instanceof Boolean) {editor. putBoolean (key, (Boolean) object);} else if (object instanceof Float) {editor. putFloat (key, (Float) object);} else if (object instanceof Long) {editor. putLong (key, (Long) object);} else {editor. putString (key, object. toString ();} SharedPreferencesCompat. apply (editor);}/*** to obtain the method for saving data. We can obtain the specific type of data to be saved Based on the default value, then call the relative method to obtain the value ** @ param context * @ par Am key * @ param defaultObject * @ return */public static Object get (Context context, String key, Object defaultObject) {SharedPreferences sp = context. getSharedPreferences (FILE_NAME, Context. MODE_PRIVATE); if (defaultObject instanceof String) {return sp. getString (key, (String) defaultObject);} else if (defaultObject instanceof Integer) {return sp. getInt (key, (Integer) defaultObject);} else if (defaultOb Ject instanceof Boolean) {return sp. getBoolean (key, (Boolean) defaultObject);} else if (defaultObject instanceof Float) {return sp. getFloat (key, (Float) defaultObject);} else if (defaultObject instanceof Long) {return sp. getLong (key, (Long) defaultObject);} return null ;} /*** remove the value corresponding to a key value ** @ param context * @ param key */public static void remove (Context context, String key) {SharedPreferences sp = Context. getSharedPreferences (FILE_NAME, Context. MODE_PRIVATE); SharedPreferences. editor editor = sp. edit (); editor. remove (key); SharedPreferencesCompat. apply (editor);}/*** clear all data ** @ param context */public static void clear (Context context) {SharedPreferences sp = context. getSharedPreferences (FILE_NAME, Context. MODE_PRIVATE); SharedPreferences. editor editor = sp. edit (); editor. clear (); SharedPreferencesC Ompat. apply (editor);}/*** query whether a key already exists ** @ param context * @ param key * @ return */public static boolean contains (Context context, string key) {SharedPreferences sp = context. getSharedPreferences (FILE_NAME, Context. MODE_PRIVATE); return sp. contains (key);}/*** returns all key-value pairs ** @ param context * @ return */public static Map <String,?> GetAll (Context context) {SharedPreferences sp = context. getSharedPreferences (FILE_NAME, Context. MODE_PRIVATE); return sp. getAll ();}/*** creates a solution for SharedPreferencesCompat. A compatible class of the apply Method ** @ author zhy **/private static class SharedPreferencesCompat {private static final Method sApplyMethod = findApplyMethod (); /*** reflect the apply method ** @ return */@ SuppressWarnings ({"unchecked", "rawtypes"}) private static Me Thod findApplyMethod () {try {Class clz = SharedPreferences. editor. class; return clz. getMethod ("apply");} catch (NoSuchMethodException e) {} return null;}/*** use apply if found, otherwise, use commit ** @ param editor */public static void apply (SharedPreferences. editor editor) {try {if (sApplyMethod! = Null) {sApplyMethod. invoke (editor); return ;}} catch (IllegalArgumentException e) {} catch (IllegalAccessException e) {} catch (InvocationTargetException e) {} editor. commit ();}}}

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.