Sharedpreferences used in Android project specification

Source: Internet
Author: User

A. What is Sharedpreferences?

Sharedpreferences is one of the four data storage technologies for Android (Sharedpreferences, SQLite, Content provider, and file), Sharedpreferences is dealing with a key-value (key-value pair), often used to store some lightweight data.

(official website is the best place to learn Android)

Website Link: http://developer.android.com/reference/android/content/SharedPreferences.html

Second, simple use

1. Create a sharecontents class to hold the Key-value key

public class Sharecontents {public static string Sharename= "ShareName";p ublic static string myName = "MyName";p ublic Stati C String isgood = "Isgood";}

2. Create a Sharemanager class that is used to manage Sharedpreferences's get and set methods uniformly, equivalent to the value set or Key-value.

Import Android.content.context;import Android.content.sharedpreferences;import Android.content.sharedpreferences.editor;public class Sharemanager {private Sharedpreferences share;private Editor Editor;private String TAG = "Sharemanager";p rivate Sharemanager () {super ();} public void Clear () {editor.clear (). commit ();}; Public Sharemanager (Context context) {super (); share = Context.getsharedpreferences (Sharecontents.sharename, context.mode_private); editor = Share.edit ();} /** * Get myName * * @return */public string Getmyname () {string result = Share.getstring (Sharecontents.myname, ""); return Result;} /** * Set MyName * * @param myName */public void Setmyname (String myName) {editor.putstring (Sharecontents.myname, myName). C Ommit ();} /** * Get Isgood * @return */public boolean Isgood () {Boolean result = Share.getboolean (Sharecontents.isgood, false);; return result;} /** * Set Isgood * @param isgood */public void Setisgood (Boolean isgood) {Editor.putboolean (Sharecontents.isgood, IsGood). Commit ();}}

3. Demo Demo of Sharedpreferences

public class Sharepreferencesactivity extends Activity {@Overrideprotected void onCreate (Bundle savedinstancestate) { Super.oncreate (savedinstancestate), final button button = New button (this), Setcontentview (button), Button.settext (" Click on the screen, you can set the sharedpreferences and get the value displayed on the screen ");//Generate object sharemanagerfinal sharemanager Sharemanager = new Sharemanager ( this);//Set name Sharemanager.setmyname ("Li Xiao Long");//Set Isgood to Truesharemanager.setisgood (true); Button.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (view view) {Button.settext ("MyName:" + Sharemanager.getmyname () + "  ; "+" Isgood: "+sharemanager.isgood ());};}}

You can click here to find the demo of this section

This article link: http://www.cnblogs.com/liqw/p/4148411.html

Sharedpreferences used in Android project specification

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.