Sputils, Access simple strings, Int,boolean and JavaBean objects

Source: Internet
Author: User

Can be combined with Gson to convert the JavaBean into a string implementation of the object access, the basic code is as follows

PackageCom.example.luozhenlonghp.project_aidl_demo;

ImportAndroid.content.Context;
ImportAndroid.content.SharedPreferences;

/**
* Use for Save data ofstring,int,boolean,list<anybean>
* Created by LUOZHENLONGHP on 2017/5/13.
*/

Public classsputils {

/**
*
* @param Context
* @param
FileName
* @param Key
* @param Value
*/
Public static voidPutstring (Context context,stringfilename,string key,string value) {
Sharedpreferences sp = context.getsharedpreferences (filename,context.mode_private);
Sharedpreferences.editor edit =sp.edit ();
Edit.putstring (Key,value);
Edit.commit ();
}

/**
*
* @param
Context
* @param FileName
* @param Key
* @return Ifget failed return "";
*/
Public StaticString getString (Context context,string filename,string key) {
Sharedpreferences sp =context.getsharedpreferences (fileName, context.mode_private);
String result = sp.getstring (key,"");
returnResult
}

Public static voidPutint (Context context,string filename,string Key,intValue) {
Sharedpreferences sp =context.getsharedpreferences (fileName, context.mode_private);
Sharedpreferences.editor edit =sp.edit ();
Edit.putint (Key,value);
Edit.commit ();
}

public static intGetInt (Context context,string filename,string key) {
Sharedpreferences sp =context.getsharedpreferences (fileName, context.mode_private);
intresult = Sp.getint (key, 0);
returnResult
}
Public static voidPutboolean (Context context,string filename,string Key,BooleanValue) {
Sharedpreferences sp =context.getsharedpreferences (fileName, context.mode_private);
Sharedpreferences.editor edit =sp.edit ();
Edit.putboolean (Key,value);
Edit.commit ();
}

Public StaticbooleanGetboolean (Context context,string filename,string key) {
Sharedpreferences sp =context.getsharedpreferences (fileName, context.mode_private);
Booleanresult = Sp.getboolean (key,false);
returnResult
}


}

Sputils, Access simple strings, Int,boolean and JavaBean objects

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.