Android often uses the tool class package---sharedpreferencesutil

Source: Internet
Author: User

Sharedpreferences is often used to save simple data, such as recording the configuration of user operations, and easy to use.

public class Sharedpreferencesutil {//stored sharedpreferences file name private static final String file_name = "Save_file_name" /** * Save data to File * @param context * @param key * @param data */public static void SaveData (context context, String Key,objec T data) {String type = Data.getclass (). Getsimplename (); Sharedpreferences sharedpreferences = context.getsharedpreferences (file_name, context.mode_private); Editor editor = Sharedpreferences.edit (), if ("Integer". Equals (Type)) {Editor.putint (key, (Integer) data);} else if ("Boolean". Equals (Type)) {Editor.putboolean (Key, (Boolean) data);} else if ("string". Equals (Type)) {editor.putstring (key, (String) data);} else if ("float". Equals (Type)) {Editor.putfloat (key, (Float) data);} else if ("Long". Equals (Type)) {Editor.putlong (key, (long) data);} Editor.commit ();} /** * Read data from a file * @param context * @param key * @param defvalue * @return */public static Object getData (context context, S Tring Key, Object defvalue) {String type = Defvalue.getclass (). Getsimplename (); SharedpreferencEs sharedpreferences = context.getsharedpreferences (file_name, context.mode_private);//defvalue is the default value, Assuming that the data is not currently fetched, return it if ("Integer". Equals (Type)) {return Sharedpreferences.getint (key, (Integer) defvalue);} else if ("Boolean". Equals (Type)) {return Sharedpreferences.getboolean (Key, (Boolean) defvalue);} else if ("string". Equals (Type)) {return sharedpreferences.getstring (key, (String) defvalue);} else if ("float". Equals (Type)) {return sharedpreferences.getfloat (key, (Float) defvalue);} else if ("Long". Equals (Type)) {return Sharedpreferences.getlong (key, (long) defvalue);} return null;}}


Android often uses the tool class package---sharedpreferencesutil

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.