Android-Developed Sharedpreferences package

Source: Internet
Author: User

For most beginners, if you want to use sharedpreferences for data storage, most people (including myself) should:

Store:

    Sharedpreferences sharedpreferences = getsharedpreferences (context.getpackagename (), Context.MODE_PRIVATE);       = Sharedpreferences.edit ();      Editor.putstring ("username", "Tom");      Editor.putint ("password", 123456);      Editor.commit ();  

Get:

    Sharedpreferences sharedpreferences = getsharedpreferences (context.getpackagename (), Context.MODE_PRIVATE);       = Sharedpreferences.getstring ("username", "");       int password = sharedpreferences.getint ("password", 0);  

Although this does not have a lot of code, if it is for beginners practice is harmless. But if you are doing a project development, it uses a large number of data storage. Each time the above code repeats over and over again, it is estimated to cause a person to crash. Therefore, for actual project development, it is best to encapsulate the package of the tool class that can be encapsulated.

The following is a packaged Sharedpreferences tool class, if you need to refer directly to OK:

 PackageCom.example.utl;ImportJava.util.Map;ImportAndroid.content.Context;Importandroid.content.SharedPreferences;/*** Save information Configuration class * *@authorAdmin*/ Public classSharedpreferenceshelper {Privatesharedpreferences sharedpreferences; /** Save the name of the phone*/     PublicString file_name = "Shared_data"; PrivateSharedpreferences.editor Editor;  PublicSharedpreferenceshelper (Context context) {Sharedpreferences=context.getsharedpreferences (file_name, context.mode_private); Editor=Sharedpreferences.edit (); }    /*** Storage*/     Public voidput (String key, Object object) {if(ObjectinstanceofString)        {editor.putstring (key, (String) object); } Else if(ObjectinstanceofInteger)        {Editor.putint (key, (Integer) object); } Else if(ObjectinstanceofBoolean)        {Editor.putboolean (key, (Boolean) object); } Else if(ObjectinstanceofFloat)        {Editor.putfloat (key, (Float) object); } Else if(ObjectinstanceofLong)        {Editor.putlong (key, (Long) object); } Else{editor.putstring (key, object.tostring ());    } editor.commit (); }    /*** Get saved data*/     PublicObject Getsharedpreference (String key, Object Defaultobject) {if(DefaultobjectinstanceofString) {            returnsharedpreferences.getstring (Key, (String) defaultobject); } Else if(DefaultobjectinstanceofInteger) {            returnSharedpreferences.getint (Key, (Integer) defaultobject); } Else if(DefaultobjectinstanceofBoolean) {            returnSharedpreferences.getboolean (Key, (Boolean) defaultobject); } Else if(DefaultobjectinstanceofFloat) {            returnsharedpreferences.getfloat (Key, (Float) defaultobject); } Else if(DefaultobjectinstanceofLong) {            returnSharedpreferences.getlong (Key, (Long) defaultobject); } Else {            returnSharedpreferences.getstring (Key,NULL); }    }    /*** Remove a value that already corresponds to a key value*/     Public voidRemove (String key) {editor.remove (key);    Editor.commit (); }    /*** Clear All data*/     Public voidClear () {editor.clear ();    Editor.commit (); }    /*** Query whether a key exists*/     PublicBoolean contain (String key) {returnsharedpreferences.contains (key); }    /*** Returns all key-value pairs*/     PublicMap<string,?>GetAll () {returnSharedpreferences.getall (); }}

Android-Developed Sharedpreferences package

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.