Share a sharedpreferences tool class to save data

Source: Internet
Author: User

Sharedpreferences is usually used to save some data, which is stored on the mobile phone. The specific path is data/your package name/shared_prefs/the saved file name. XML and sharedpreferences are easy to use. I wrote a sharedpreferences tool class and saved it here. I will copy the code from here when I need to save the data. Haha

Tool class:

Package COM. example. export cut; import android. content. context; import android. content. sharedpreferences;/*** a tool class of sharedpreferences. You can call setparam to save string, integer, Boolean, float, long parameters * Call getparam to obtain the data stored in the mobile phone * @ author xiaanming **/public class sharedpreferencesutils {/*** file name saved in the mobile phone * /Private Static final string file_name = "1__date "; /*** to save the data, we need to obtain the specific type of the data to be saved, then, call Different Storage Methods Based on the type * @ Param context * @ Param key * @ Param object */public static void setparam (context, string key, object) {string type = object. getclass (). getsimplename (); sharedpreferences sp = context. getsharedpreferences (file_name, context. mode_private); sharedpreferences. editor editor = sp. edit (); If ("string ". equals (type) {editor. putstring (Key, (string) object);} else if ("integer ". equals (type) {editor. putint (Key, (integer) object);} else if ("Boolean ". equals (type) {editor. putboolean (Key, (Boolean) object);} else if ("float ". equals (type) {editor. putfloat (type, (float) object);} else if ("long ". equals (type) {editor. putlong (type, (long) object);} editor. commit ();}/*** to get the method for saving data. We can get the specific type of data to be saved Based on the default value, then, call the relative method to obtain the value * @ Param context * @ Param key * @ Param defaultobject * @ return */public static object getparam (context, string key, object defaultobject) {string type = defaultobject. getclass (). getsimplename (); sharedpreferences sp = context. getsharedpreferences (file_name, context. mode_private); If ("string ". equals (type) {return sp. getstring (Key, (string) defaultobject);} else if ("integer ". equals (type) {return sp. getint (Key, (integer) defaultobject);} else if ("Boolean ". equals (type) {return sp. getboolean (Key, (Boolean) defaultobject);} else if ("float ". equals (type) {return sp. getfloat (type, (float) defaultobject);} else if ("long ". equals (type) {return sp. getlong (type, (long) defaultobject) ;}return null ;}}

It is easy to use and save data.

SharedPreferencesUtils.setParam(this, "String", "xiaanming");SharedPreferencesUtils.setParam(this, "int", 10);SharedPreferencesUtils.setParam(this, "boolean", true);SharedPreferencesUtils.setParam(this, "long", 100L);SharedPreferencesUtils.setParam(this, "float", 1.1f);

Get Data

SharedPreferencesUtils.getParam(TimerActivity.this, "String", "");                                                                                        SharedPreferencesUtils.getParam(TimerActivity.this, "int", 0);SharedPreferencesUtils.getParam(TimerActivity.this, "boolean", false);SharedPreferencesUtils.getParam(TimerActivity.this, "long", 0L);SharedPreferencesUtils.getParam(TimerActivity.this, "float", 0.0f);

Is it very convenient? I hope you can help me read the article a little bit!

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.