Android data store (1): sharedpreferences

Source: Internet
Author: User
<span id="Label3"></p><p><p><span style="color: #ff0000;"><strong>The Android system provides 5 ways to store application Data (application), which are sharedpreferences (shared parameters), Internal Storage (internal storage), External Storage ( External storage), SQLite Databases (database storage), network Connection (networked storage), can be used according to different scenarios to use the appropriate storage Method.</strong></span></p></p><p><p></p></p><p><p><span style="color: #ff0000;">  <span style="color: #000000;">is part of the data storage from the Android 5.0 official documentation:</span></span></p></p><p><p><span style="color: #000000;"><strong>  </strong>  </span></p></p><p><p><span style="color: #000000;">From the known <span style="color: #ff0000;">sharedpreferences</span> mainly in the form of <span style="color: #ff0000;">Key-value pairs</span> to store <span style="color: #ff0000;">private data</span> ,<span style="color: #ff0000;">Internam Storage</span> is mainly in the <span style="color: #ff0000;">memory</span> of the phone storage <span style="color: #ff0000;">private Data</span> ,<span style="color: #ff0000;">External Storage</span> is mainly in the Phone's <span style="color: #ff0000;">expansion card</span> (sd card, memory Card) to store <span style="color: #ff0000;">public data</span> , and <span style="color: #ff0000;">SQLite Databases</span> is to store data that has a data <span style="color: #ff0000;">structure</span> in a <span style="color: #ff0000;">private database</span> , while <span style="color: #ff0000;">NetWork Connection</span> stores data on its own <span style="color: #ff0000;">server</span> .</span></p></p><p><p><span style="color: #000000;">  </span></p></p><p><p><span style="color: #000000;"> <strong>sharedpreferences, also known as <span style="color: #ff0000;">shared parameters</span> , is a lightweight storage class for storing small amounts of software configuration parameter information, which stores data in an XML file, where the file is stored in The/data/data/package name/shared_ The Pref directory.</strong></span></p></p><p><p><span style="color: #000000;"><strong>  </strong>Check the official documentation to know Shredpreferences:</span></p></p><p><p><span style="color: #000000;">  </span></p></p><p><p><span style="color: #000000;">According to the above, Sharedpreferences is an interface that creates an object using a singleton pattern, that is, at any moment, there is only one Sharedpreferences object in Memory.</span></p></p><p><p></p></p><p><p><span style="color: #000000;">  <strong>Sharedpreferences is an interface that internally defines two common internal interfaces:</strong></span></p></p><p><p><span style="color: #000000;">  </span></p></p><p><p><span style="color: #000000;">    </span></p></p><p><p><span style="color: #000000;">  <strong>Sharedpreferences defines the Method:</strong></span></p></p><p><p><span style="color: #000000;">  </span></p></p><p><p></p></p><p><p><span style="color: #000000;">  <strong>The whole definition of the sharedpreferences class:</strong></span></p></p><p><p><span style="color: #000000;">  </span></p></p><p><p></p></p><p><p><span style="color: #000000;">According to the above summary:</span></p></p><p><p><span style="color: #000000;">1. <strong>Get the Sharedpreferences object</strong> :</span></p></p><p><p><span style="color: #000000;">1) <span style="color: #ff0000;">context.getsharedpreferences (String name,int Mode) Returns a sharedpreferences object,</span> </span> <span style="color: #ff0000;">can be shared by other components under the same application</span> .</p></p><p><p><span style="color: #000000;">Eg:sharedpreferences sp = getsharedpreferences ("shared_pref", mode_private);</span></p></p><p><p><span style="color: #000000;">Name is the file name of the data saved by sharedpreferences, there is no suffix, and mode is the stored pattern, there are four main types:</span></p></p><p><p><span style="color: #000000;">     </span></p></p><p><p><span style="color: #000000;">The value of mode is four (API 17), respectively:</span></p></p><p><p><span style="color: #000000;">Mode_private: Private</span></p></p><p><p><span style="color: #000000;">Mode_apped: Append</span></p></p><p><p><span style="color: #000000;">Mode_enable_write_ahead_logging:</span></p></p><p><p><span style="color: #000000;">Mode_multi_process:</span></p></p><p><p><span style="color: #000000;">where Mode_world_readable (global read) and mode_world_writeable (global Write) are Obsolete.</span></p></p><p><p><span style="color: #000000;">2)<span style="color: #ff0000;">activity.getpreferences (int Mode) Returns a Sharedpreferences object that can be used only in that Activity. The file name of the saved data is automatically named the Activity's name, for example, mainactivity.xml</span>.<br></span></p></p><p><p><span style="color: #000000;">      </span></p></p><p><p><span style="color: #000000;">2. Save data and fetch data from Sharedpreferences.</span></p></p><p><p><span style="color: #000000;">    <strong>The data can be used to directly use the sharedpreferences Object's getxxx () method, such as Getint (), Getlong (), getString (), and so on, the key of the Key-value Pair.</strong></span></p></p><p><p><strong><span style="color: #000000;">To save the data is to get Sharedpreferences's internal class editor object, which is an edit object, and then use the putxxx () method, such as Putint (), Putlong (), putstring (), and so on, parameters are key-value pairs of keys and values, finally, It is important to note that the data must use the commit () or the Apply method to save Successfully.</span></strong></p></p><p><p><span style="color: #000000;">eg</span></p></p><p><p><span style="color: #000000;">    <span style="color: #ff0000;"><strong>Sharedpreferences sp = getsharedpreferences ("shared_pref", mode_private);</strong></span></span></p></p><p><p><span style="color: #ff0000;"><strong>Sharedpreferences.editor Editor = Sp.editor ();</strong></span></p></p><p><p><span style="color: #000000;">Editor.putstring ("name", "Tom");</span></p></p><p><p><span style="color: #000000;">Editor.putint ("age", 13);</span></p></p><p><p><span style="color: #000000;">Editor.commit ();</span></p></p><p><p><span style="color: #000000;">----------------------------------------------------------------------------------------</span></p></p><p><p><span style="color: #000000;">String name = sp.getstring ("name", null);</span></p></p><p><p><span style="color: #000000;">int age = Sp.getint ("age",-1);</span></p></p><p><p></p></p><p><p>  <strong>Not finished, to be continued.</strong></p></p><p><p>Android data store (1): sharedpreferences</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.