Usage of sharedpreferences in Android

Source: Internet
Author: User
<span id="Label3"></p><p><p>Do software development should know, a lot of software will have configuration files, which is stored in the program running the various property values, because of its configuration information is not much, if the use of database to store is not cost-effective, because the database connection and operation time greatly affected the efficiency of the program, So we use the corresponding relationship of the key value to store these configuration Information. Sharedpreferences is the technology used in Android to implement this storage method.</p></p><p><p></p></p><p><p>The sharedpreferences is simple to use and can easily store data and read Data. Sharedpreferences can save only simple types of data, such as String, int, and so On. It is common to convert data of complex types to Base64 encoding, then save the converted data as a string in an XML file, and then save it with Sharedpreferences.</p></p><p><p></p></p>The steps to save the Key-value pair using sharedpreferences are as Follows:<p><p>(1) Use the Getsharedpreferences method of the activity class to get the Sharedpreferences object, where the name of the file that stores the Key-value is specified by the first parameter of the Getsharedpreferences method.</p></p><p><p>(2) use edit of Sharedpreferences interface to get Sharedpreferences.editor object.</p></p><p><p>(3) the Putxxx method of Sharedpreferences.editor interface is used to save the Key-value Pair. where xxx represents a different data type. For example, the value of string type requires the putstring method.</p></p><p><p>(4) The Key-value pair is saved by the commit method of the Sharedpreferences.editor Interface. The Commit method is equivalent to a commit operation in a database Transaction.</p></p><p><p></p></p><p><p>The writing process for the specific code is:</p></p><pre><pre><span style="color: #0000ff;">protected</span> <span style="color: #0000ff;">void</span> <span style="color: #000000;">onCreate (Bundle Savedinstancestate) {... pref</span>=preferencemanager.getdefaultsharedpreferences (this<span style="color: #0000ff;"> </span>);</pre></pre><p><p></p></p><p><p>Add Preferencemanager to OnCreate</p></p><p><p>Adding code to the onclick</p></p><p><p></p></p><pre>String account =<span style="color: #000000;"><span style="color: #000000;">username.gettext (). toString (); String Pass</span></span>=<span style="color: #000000;"><span style="color: #000000;">userpass.gettext (). toString (); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(account.equals ("admin") && pass.equals ("123456"<span style="color: #000000;">) <span style="color: #000000;">{editor</span></span>=<span style="color: #000000;"><span style="color: #000000;">Pref.edit (); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span><span style="color: #000000;"><span style="color: #000000;">(remember.ischecked ()) {editor.putboolean (</span></span>"remember_pass",<span style="color: #0000ff;"><span style="color: #0000ff;">true</span></span><span style="color: #000000;"><span style="color: #000000;">); Editor.putstring (</span></span>"username"<span style="color: #000000;"><span style="color: #000000;">, account); Editor.putstring (</span></span>"userpass"<span style="color: #000000;"><span style="color: #000000;">, pass); }</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Else</span></span><span style="color: #000000;"><span style="color: #000000;">{editor.clear (); } editor.commit ();</span></span></pre><p><p></p></p><p><p></p></p><p><p>Case Http://pan.baidu.com/s/1skWaSPJ</p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p>Usage of sharedpreferences in Android</p></p></span>

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.