Example of sharedpreferences storage obtaining information

Source: Internet
Author: User

Sharedpreferences is easy to use and can easily store and read data. Sharedpreferences can only store simple types of data, such as string and Int. Generally, the data of complex types is converted to base64 encoding, And the converted data is saved as a string in the XML file, and then saved in sharedpreferences.
 
To save the key-Value Pair using sharedpreferences, follow these steps:
(1) Use the getsharedpreferences method of the activity class to obtain the sharedpreferences object. The name of the file storing the key-value is specified by the first parameter of the getsharedpreferences method.
(2) Use the edit interface of the sharedpreferences interface to obtain the sharedpreferences. Editor object.
(3) Use the putxxx method of the sharedpreferences. Editor interface to save the key-value pair. Xxx indicates different data types. For example, the putstring method is required for string-type values.
(4) use the commit method of the sharedpreferences. Editor interface to save the key-value pair. The commit method is equivalent to a commit operation in a database transaction.


I made a small example this morning. The following upload code: This project contains two java files.

The mypreferenceactivity. Java file is as follows: This file is mainly used to set the interface and store parameter values.

Public class mypreferenceactivity extends preferenceactivity implements extends {private edittextpreference baudrate; private edittextpreference port; private edittextpreference group; @ overrideprotected void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); // Add the preferences layout file. The method of adding the preferences to the general layout file is different. Note the difference between addpreferencesfromresource (R. layout. preference); // get a shared preferences. Use this variable to read the parameter values in the configuration file sharedpreferences sp = getpreferencemanager (). getsharedpreferences (); baudrate = (edittextpreference) findpreference ("set_baudrate"); baudrate. setonpreferencechangelistener (this); Port = (edittextpreference) findpreference ("set_serialport"); Port. setonpreferencechangelistener (this); group = (edittextpreference) findpreference ("set_other"); group. setonpreferencechangelistener (this); // The getstring method of SP has two parameters. This method is used to obtain the configuration parameters. Parameter 1 is the preferred parameter, which is generally in the configuration file of Chengdu, parameter 2 is the default parameter. If parameter 1 cannot be read, use parameter 2: String baudratestr = sp. getstring ("set_baudrate", getresources (). getstring (R. string. IP); baudrate. setsummary (baudratestr); baudrate. settext (baudratestr); string portstr = sp. getstring ("set_serialport", getresources (). getstring (R. string. p); Port. setsummary (portstr); Port. settext (portstr); string groupstr = sp. getstring ("set_other", getresources (). getstring (R. string. group); group. setsummary (groupstr); group. settext (groupstr) ;}@ overridepublic Boolean onpreferencechange (preference, object newvalue) {If (preference = baudrate) {preference. setsummary (newvalue. tostring ();} If (preference = port) {preference. setsummary (newvalue. tostring ();} If (preference = group) {preference. setsummary (newvalue. tostring () ;}return true ;}}

The setactivity. Java file is as follows:

Public class setactivity extends activity {sharedpreferences preferences;/* sharedpreferences. editor editor; */@ overrideprotected void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); // obtain the sharedpreferences object preferences = getsharedpreferences ("com. hiden. settest_preferences ", mode_world_readable); button READ = (button) findviewbyid (R. id. Read); read. setonclicklistener (New onclicklistener () {@ overridepublic void onclick (view arg0) {log. D ("SP", preferences. getstring ("set_baudrate", "NONE"); // read string data string baudrate = preferences. getstring ("set_baudrate", null); string port = preferences. getstring ("set_serialport", null); string group = preferences. getstring ("set_other", null); string result = "baud rate value:" + baudrate + "port value:" + port + "other values: "+ G Roup; // use toast to indicate toast. maketext (setactivity. this, results, 5000 ). show () ;}}) ;}@ overridepublic Boolean oncreateoptionsmenu (menu) {menuinflater inflator = new menuinflater (this); // status R. menu. the context menu, and add it to the menu inflator. inflate (R. menu. myset, menu); return Super. oncreateoptionsmenu (menu) ;}@ override // callback method after the menu item is clicked public Boolean onoptionsitemselected (menumi item) {If (MI. ischeckable () {mi. setche Cked (true); // ②} // determines which menu item is clicked and responds accordingly. Switch (MI. getitemid () {case R. id. plain_item: // create intentintent intent = new intent (setactivity. this, mypreferenceactivity. class); // start activitystartactivity (intent); break;} return true ;}}

This program is used to start the main interface, click the button to get
Storage information of sharedpreferences. <Span style = "background-color: RGB (188,211,229); font-family: simsun;"> preference. XML code: </span>
<PRE name = "code" class = "html"> <? XML version = "1.0" encoding = "UTF-8"?> <Preferencescreen xmlns: Android = "http://schemas.android.com/apk/res/android" Android: Title = "Settings"> <preferencecategory Android: Title = "Settings"> <edittextpreference Android: Key = "set_baudrate" Android: title = "@ string/baudrate"/> <edittextpreference Android: Key = "set_serialport" Android: Numeric = "integer" Android: title = "@ string/serial_interface"/> <edittextpreference Android: Key = "set_other" Android: Title = "@ string/other" Android: dialogtitle = "@ string/ht_group"/> </preferencecategory> </preferencescreen>


 

The main interface is as follows:

Use actionbar to add settings at the top of the interface, and click Settings to display the following interface

Click Read. The following page is displayed:

In this way, the stored data can be obtained, the source code can be downloaded from here: http://download.csdn.net/detail/liaction/7694989

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.