<span id="Label3"></p><p><p>The haredpreferences 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>The steps to save the Key-value pair using sharedpreferences are as Follows:</p></p> <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> <pre><pre><span style="color: #0000ff"></span> public <span style="color: #0000ff">Static</span> <span style="color: #0000ff">Final</span> String Key_token = "TOKEN"<span style="color: #000000">; </span> <span style="color: #0000ff"></span> public <span style="color: #0000ff">Static</span> <span style="color: #0000ff">Final</span> String app_id = "com.zhulang.secret"<span style="color: #000000">; </span> <span style="color: #008000">//read</span></pre></pre> <pre><pre><span style="color: #008000"></span> <span style="color: #0000ff"></span> public <span style="color: #0000ff">Static</span> <span style="color: #000000">String getcachedtoken (context Context) { </span><span style="color: #0000ff">return</span><span style="color: #0000ff">null</span><span style="color: #000000">); } Store Tokens.</span></pre></pre> <pre><pre><span style="color: #000000"> </span><span style="color: #0000ff"></span> public <span style="color: #0000ff">Static</span> <span style="color: #0000ff">void</span> <span style="color: #000000">cachedtoken (context context, String token) { </span>=<span style="color: #000000"> context.getsharedpreferences (app_id,context. mode_private). Edit (); Et.putstring (key_token,token); Et.commit (); }</span></pre></pre><p><p>Sharedpreferences Notes from Android</p></p></span>
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