Android uses sharedpreferences data storage

Source: Internet
Author: User

I wrote an SP helper class.

Although the writing is a bit verbose, but also their own results. As follows:

Package Com.yqy.yqy_testsputil;import Android.annotation.suppresslint;import Android.app.activity;import Android.content.context;import android.content.sharedpreferences;/** * SP Helper class * @author YQY * * */@SuppressLint (" Commitprefedits ") public class Sputil {private static sharedpreferences msp;private static String spname =" Yqy_testsputil ";/** * put * @param context * @param name key * @param object value */public static void put (context context,string name,obj ECT object) {MSP = Context.getsharedpreferences (spname, activity.mode_private); Sharedpreferences.editor Editor = Msp.edit (); if (object instanceof String) {editor.putstring (name, (String) object);} else if (object instanceof Integer) {editor.putint (name, (Integer) object);} else if (object instanceof Long) {Editor.putlong (name, (Long) object);} else if (object instanceof Float) {editor.putfloat (name, (Float) object);} else if (object instanceof Boolean) {Editor.putboolean (name, (Boolean) object);} Editor.commit ();} public static void Set (context context, string what, String value) {sharedpreferences sharedpreferences = context.getsharedpreferences (spname, 0); Sharedpreferences.edit (). putstring (What, Value). commit ();} public static String getString (Context context,string name) {MSP = Context.getsharedpreferences (spname, Activity.mode_ PRIVATE); return msp.getstring (Name, "");} public static int getInt (Context context,string name) {MSP = Context.getsharedpreferences (spname, Activity.mode_private ); return msp.getint (name, 0);} public static Long Getlong (Context context,string name) {MSP = Context.getsharedpreferences (spname, Activity.mode_ PRIVATE); return Msp.getlong (name, 0);} public static Float getfloat (Context context,string name) {MSP = Context.getsharedpreferences (spname, Activity.mode_ PRIVATE); return msp.getfloat (name, 0);} public static Boolean Getboolean (Context context,string name) {MSP = Context.getsharedpreferences (spname, Activity.mode _private); return Msp.getboolean (name, false);}}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Android uses sharedpreferences data storage

Related Article

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.