Sharepreference of Android Tool class

Source: Internet
Author: User

1 /** 2 * Sharedpreferences of a tool class, call SetParam can save string, Integer, Boolean, Float, long type parameters3 * Also call GetParam to get the data stored in the phone4  * @authorxiaanming5  * 6  */  7  Public classSharedpreferencesutils {8     /** 9 * file name stored in the phoneTen      */   One     Private Static FinalString file_name = "Share_date";  A        -        -     /**  the * method of saving data, we need to get the specific type of the data to be saved, then call different save methods according to the type -      * @paramContext -      * @paramKey -      * @paramObject +      */   -      Public Static voidSetParam (context context, String key, Object object) { +            AString type =Object.getclass (). Getsimplename ();  atSharedpreferences SP =context.getsharedpreferences (file_name, context.mode_private);  -Sharedpreferences.editor Editor =Sp.edit ();  -            -         if("String". Equals (Type) {   - editor.putstring (Key, (String) object);  -         }   in         Else if("Integer". Equals (Type) {   - Editor.putint (Key, (Integer) object);  to         }   +         Else if("Boolean". Equals (Type) {   - Editor.putboolean (Key, (Boolean) object);  the         }   *         Else if("Float". Equals (Type) {   $ editor.putfloat (Key, (Float) object); Panax Notoginseng         }   -         Else if("Long". Equals (Type) {   the Editor.putlong (Key, (Long) object);  +         }   A            the Editor.commit ();  +     }   -        $        $     /**  - * Get the method to save the data, we are based on the default value to save the specific type of data, and then call the relative method to get the values -      * @paramContext the      * @paramKey -      * @paramDefaultobjectWuyi      * @return  the      */   -      Public StaticObject GetParam (context context, String key, Object defaultobject) { WuString type =Defaultobject.getclass (). Getsimplename ();  -Sharedpreferences SP =context.getsharedpreferences (file_name, context.mode_private);  About            $         if("String". Equals (Type) {   -             returnsp.getstring (Key, (String) defaultobject);  -         }   -         Else if("Integer". Equals (Type) {   A             returnSp.getint (Key, (Integer) defaultobject);  +         }   the         Else if("Boolean". Equals (Type) {   -             returnSp.getboolean (Key, (Boolean) defaultobject);  $         }   the         Else if("Float". Equals (Type) {   the             returnsp.getfloat (Key, (Float) defaultobject);  the         }   the         Else if("Long". Equals (Type) {   -             returnSp.getlong (Key, (Long) defaultobject);  in         }   the            the         return NULL;  About     }   the}

Save data:

    Sharedpreferencesutils.setparam (This, "String", "xiaanming");      Sharedpreferencesutils.setparam (this, "int", ten);      Sharedpreferencesutils.setparam (Thistrue);      Sharedpreferencesutils.setparam (This, "long", 100L);      Sharedpreferencesutils.setparam (This, "float", 1.1f);  

Get Data:

 1  Sharedpreferencesutils.getparam (timeractivity.this , "String", ""); Sharedpreferencesutils.getparam (Timeractivity.  This , "int", 0);  2  Sharedpreferencesutils.getparam (timeractivity.this , "boolean", false  );  3  Sharedpreferencesutils.getparam (timeractivity.this , "Long", 0l);  4  Sharedpreferencesutils.getparam (timeractivity.this , "float", 0.0f); 
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.