Android sharedpreferences two tool classes

Source: Internet
Author: User

Believe that Android is the simplest way to store people are familiar with, but there is a small trick, perhaps you have not used, today to share with you, we can put sharedpreferences in a tool class, when we need to write data and read data, Can be directly through the tool class set and get method to complete, similar to JavaBean, so it is more convenient to use, fast (recommended items in the use of more than a few times). OK, just take a look at this simple piece of code:

 Public classSharepreferenceutil {PrivateSharedpreferences sp; PrivateSharedpreferences.editor Editor;  PublicSharepreferenceutil (context context, String file) {SP=context.getsharedpreferences (file, context.)        Mode_private); Editor=Sp.edit (); }    //User's password     Public voidsetpasswd (String passwd) {editor.putstring ("passwd", passwd);    Editor.commit (); }     PublicString getpasswd () {returnSp.getstring ("passwd",""); }    //the user's ID, which is the QQ number     Public voidsetId (String id) {editor.putstring ("ID", id);    Editor.commit (); }     PublicString getId () {returnSp.getstring ("ID",""); }    //User's nickname     PublicString GetName () {returnSp.getstring ("name",""); }     Public voidsetName (String name) {editor.putstring ("name", name);    Editor.commit (); }    //User's mailbox     PublicString Getemail () {returnSp.getstring ("Email",""); }     Public voidsetemail (String email) {editor.putstring ("Email", email);    Editor.commit (); }    //user's own avatar     PublicInteger getimg () {returnSp.getint ("img",0); }     Public voidSetimg (inti) {editor.putint ("img", i);    Editor.commit (); }    //IP     Public voidsetIp (String IP) {editor.putstring ("IP", IP);    Editor.commit (); }     PublicString GetIP () {returnSp.getstring ("IP", CONSTANTS.SERVER_IP); }    //Port     Public voidSetport (intPort) {Editor.putint ("Port", Port);    Editor.commit (); }     Public intGetport () {returnSp.getint ("Port", Constants.server_port); }    //whether to run the tag in the background     Public voidSetisstart (Boolean Isstart) {Editor.putboolean ("Isstart", Isstart);    Editor.commit (); }     PublicBoolean Getisstart () {returnSp.getboolean ("Isstart",false); }    //whether to run this application for the first time     Public voidSetisfirst (Boolean IsFirst) {Editor.putboolean ("IsFirst", IsFirst);    Editor.commit (); }     PublicBoolean Getisfirst () {returnSp.getboolean ("IsFirst",true); }}

The second method is more simple and convenient: when the value is only used in the context and the corresponding key, you can get the corresponding value, set the value, it is also passed in the context and corresponding key and value, it is very convenient and practical.

 Public classPreferenceutils { Public Staticstring getprefstring (context context, String key, final string defaultvalue) {final Sharedpreferen CES settings=Preferencemanager. Getdefaultsharedpreferences (context); returnsettings.getstring (key, DefaultValue); }     Public Static voidsetprefstring (Context context, final string key, final string value) {Final sharedpreferences sett Ings=Preferencemanager. Getdefaultsharedpreferences (context);    Settings.edit (). putstring (key, value). commit (); }     Public StaticBoolean Getprefboolean (context context, final String key, final Boolean defaultvalue) {final Share Dpreferences Settings=Preferencemanager. Getdefaultsharedpreferences (context); returnSettings.getboolean (key, DefaultValue); }     Public StaticBoolean Haskey (context context, final String key) {returnpreferencemanager.getdefaultsharedpreferences (context). Contains (key); }     Public Static voidSetprefboolean (Context context, final String key, final Boolean value) {final sharedpreferences SE Ttings=Preferencemanager. Getdefaultsharedpreferences (context);    Settings.edit (). Putboolean (key, value). commit (); }     Public Static voidSetprefint (Context context, final String key, finalintvalue) {Final sharedpreferences settings=Preferencemanager. Getdefaultsharedpreferences (context);    Settings.edit (). Putint (key, value). commit (); }     Public Static intGetprefint (Context context, final String key, finalintDefaultValue) {Final sharedpreferences settings=Preferencemanager. Getdefaultsharedpreferences (context); returnSettings.getint (key, DefaultValue); }     Public Static voidSetpreffloat (Context context, final String key, finalfloatvalue) {Final sharedpreferences settings=Preferencemanager. Getdefaultsharedpreferences (context);    Settings.edit (). Putfloat (key, value). commit (); }     Public Static floatGetpreffloat (Context context, final String key, finalfloatDefaultValue) {Final sharedpreferences settings=Preferencemanager. Getdefaultsharedpreferences (context); returnsettings.getfloat (key, DefaultValue); }     Public Static voidSetsettinglong (Context context, final String key, finalLongvalue) {Final sharedpreferences settings=Preferencemanager. Getdefaultsharedpreferences (context);    Settings.edit (). Putlong (key, value). commit (); }     Public Static LongGetpreflong (Context context, final String key, finalLongDefaultValue) {Final sharedpreferences settings=Preferencemanager. Getdefaultsharedpreferences (context); returnSettings.getlong (key, DefaultValue); }     Public Static voidclearpreference (Context context, final sharedpreferences p) {Final Editor editor=P.edit ();        Editor.clear ();    Editor.commit (); }}

Android sharedpreferences two tool classes

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.