<span id="Label3"></p><p><p></p></p> <ul> <ul> <li>Brief introduction</li> <li>Storing data in a sharedpreferences <ul> <li>Get Sharedpreferences Object <ul> <li>The Getsharedpreferences method in the context class <ul> <li>File location</li> </ul></li> <li>The Getpreferences method in activity class</li> <li>Getdefaultsharedpreferences methods in the Preferencemanager class</li> </ul></li> <li>Saving data to a sharedpreferences file</li> </ul></li> <li>Reading data from the Sharedpreferences</li> <li>Results Demo</li> </ul> </ul><p><p></p></p>Brief introduction<p><p>Sharedpreferences is one of the persistence technologies in the data storage scheme (other file storage, database storage, etc.), which is a lightweight storage class that is especially suitable for saving software configuration parameters or saving temporary Data. The data is stored using sharedpreferences, which is stored in an XML file (a key-value pair), which is stored in a directory in the file system <code>/data/data/<package name>/shared_prefs</code> .</p></p>To store the data in sharedpreferences to get the Sharedpreferences object<p><p>To use Sharedpreferences to save data, you first need to get the sharedpreferences object, with the following three methods:</p></p>The Getsharedpreferences () method in the context class<p><p>Getsharedpreferences () receives two parameters: the first parameter is used to specify the name of the file (if it does not exist, it is automatically created without a suffix, the system is automatically added), the second parameter specifies the mode of operation of the file, there are two main modes: mode_private and Mode_ multi_process,mode_private, like the direct 0 effect, means that only the current application can read and write to the sharedpreferences file; mode_multi_ Process is typically used by multiple processes to read and write to the same sharedpreferences File. In fact, there are two other kinds, but has been deprecated after the Android4.2 version, not much Introduction. The simple code is as Follows:</p></p><pre class="prettyprint"><pre class="prettyprint"><code class="language-android hljs avrasm">SharedPreferences sharedPreferences = getApplicationContext()<span class="hljs-preprocessor">.getSharedPreferences</span>(<span class="hljs-string">"sywyg"</span>,<span class="hljs-number">0</span>)<span class="hljs-comment">;</span></code></pre></pre><p><p>Note:</p></p> <ul> <ul> <li>The Sharedpreferences object obtained by this method can be accessed by all activity in the same application</li> <li>File saved as XML type</li> </ul> </ul>File location<p><p>You can view the SD card file (file Explorer) with the green Android robot on the toolbar in Android studio, Ddms (as If it were written directly in eclipse), my sharedpreferences files are stored in:/ Data/data/com.sywyg.intent_test/shared_prefs/name is Sywyg.xml. After the export XML file opens, the following contents are:</p></p><pre class="prettyprint"><pre class="prettyprint"><code class="language-xml hljs "><span class="hljs-pi"><?xml version=‘1.0‘ encoding=‘utf-8‘ standalone=‘yes‘ ?></span><span class="hljs-tag"><<span class="hljs-title">map</span>></span> <span class="hljs-tag"><<span class="hljs-title">string</span> <span class="hljs-attribute">name</span>=<span class="hljs-value">"name"</span>></span>sywyg<span class="hljs-tag"></<span class="hljs-title">string</span>></span> <span class="hljs-tag"><<span class="hljs-title">long</span> <span class="hljs-attribute">name</span>=<span class="hljs-value">"id"</span> <span class="hljs-attribute">value</span>=<span class="hljs-value">"1"</span> /></span><span class="hljs-tag"></<span class="hljs-title">map</span>></span></code></pre></pre><p><p>You can see that sharedpreferences is saving data by Key-value Pairs.</p></p>The Getpreferences () method in the Activity class<p><p>The second way to get a sharedpreferences object: the Getpreferences () method in the activity class. The method accepts only one schema parameter, and the file name is automatically the class name of the current Activity. The simple code is as Follows:</p></p><pre class="prettyprint"><pre class="prettyprint"><code class="language-android hljs fix"><span class="hljs-attribute">SharedPreferences sharedPreferences2 </span>=<span class="hljs-string"> this.getPreferences(0);</span></code></pre></pre><p><p>The sharedpreferences file created in this way is stored in the same location, and the XML name Is: mainactivity.xml, which reads as Follows:</p></p><pre class="prettyprint"><pre class="prettyprint"><code class="language-xml hljs "><span class="hljs-pi"><?xml version=‘1.0‘ encoding=‘utf-8‘ standalone=‘yes‘ ?></span><span class="hljs-tag"><<span class="hljs-title">map</span>></span> <span class="hljs-tag"><<span class="hljs-title">string</span> <span class="hljs-attribute">name</span>=<span class="hljs-value">"name"</span>></span>sywyg<span class="hljs-tag"></<span class="hljs-title">string</span>></span> <span class="hljs-tag"><<span class="hljs-title">long</span> <span class="hljs-attribute">name</span>=<span class="hljs-value">"id"</span> <span class="hljs-attribute">value</span>=<span class="hljs-value">"1"</span> /></span><span class="hljs-tag"></<span class="hljs-title">map</span>></span></code></pre></pre><p><p>Note:</p></p> <ul> <ul> <li>The Sharedpreferences object obtained by this method can only be obtained through getsharedpreferences () in the context class if other activity wants to obtain the sharedpreferences File. method to get the sharedpreferences object, and of course the file name must be the Sharedpreferences file Name.</li> </ul> </ul>The Getdefaultsharedpreferences () method in the Preferencemanager class<p><p>The method is a static method, has only one context parameter, and automatically names the sharedpreferences file with the current Application's package name as a prefix. The simple code is as Follows:</p></p><pre class="prettyprint"><pre class="prettyprint"><code class="language-android hljs fix"><span class="hljs-attribute">SharedPreferences sharedPreferences3 </span>=<span class="hljs-string"> PreferenceManager.getDefaultSharedPreferences(getApplicationContext());</span></code></pre></pre><p><p>The sharedpreferences file created in this way is stored in the same location, and the XML name Is: com.sywyg.intent_test_preferences.xml, which reads as Follows:</p></p><pre class="prettyprint"><pre class="prettyprint"><code class="language-xml hljs "><span class="hljs-pi"><?xml version=‘1.0‘ encoding=‘utf-8‘ standalone=‘yes‘ ?></span><span class="hljs-tag"><<span class="hljs-title">map</span>></span> <span class="hljs-tag"><<span class="hljs-title">string</span> <span class="hljs-attribute">name</span>=<span class="hljs-value">"name"</span>></span>sywyg<span class="hljs-tag"></<span class="hljs-title">string</span>></span> <span class="hljs-tag"><<span class="hljs-title">long</span> <span class="hljs-attribute">name</span>=<span class="hljs-value">"id"</span> <span class="hljs-attribute">value</span>=<span class="hljs-value">"1"</span> /></span><span class="hljs-tag"></<span class="hljs-title">map</span>></span></code></pre></pre>Saving data to a sharedpreferences file<p><p>After getting the sharedpreferences object, you can save the data to the Sharedpreferences file, implemented in the following three steps:</p></p> <ol> <ol> <li>Call the edit () method of the Sharedpreferences object to get a sharedpreferences.editor (inner class) object;</li> <li>Adding data to the Editor object through the putxxx () method;</li> <li>Call the commit () method to commit the data and complete the data store Operation.</li> </ol> </ol><p><p>Note:</p></p> <ul> <ul> <li>Data is typically saved in the OnPause () method.</li> </ul> </ul><p><p>The specific code is as Follows:</p></p><pre class="prettyprint"><pre class="prettyprint"><code class="language-java hljs"><span class="hljs-annotation"> @Override </span> <span class="hljs-keyword">protected </span> <span class="hljs-keyword">void </span> <span class="hljs-title">onPause </span> () {<span class="hljs-keyword">super </span>. onPause (); Sharedpreferences sharedpreferences = getapplicationcontext (). getsharedpreferences (<span class="hljs-string" and " sywyg "< span>, <span class=" hljs-number ">0 </span>); <span class="hljs-comment">//sharedpreferences sharedpreferences = this.getpreferences (0); </span> <span class="hljs-comment">//sharedpreferences sharedpreferences = preferencemanager.getdefaultsharedpreferences ( Getapplicationcontext ()); </span> Sharedpreferences.editor Editor = Sharedpreferences.edit (); Editor.putstring (<span class="hljs-string"> "name" </span>, <span class="hljs-string"> "sywyg" </span>); Editor.putlong (<span class="hljs-string"> "id" </span>, <span class="hljs-number">1 </span>); Editor.commit ();} </span></code> </pre></pre><p><p>Note:</p></p> <ul> <ul> <li>The data type saved by the Putxxx () method can only be: string,int,boolean,long,float, or it can be a String collection.</li> </ul> </ul>Reading data from the Sharedpreferences<p><p>The corresponding data can be obtained by GetXXX () of the sharedpreferences object, the GetXXX () method receives two parameters, the first parameter is the key (the key name used when the data is stored), and the second parameter is the default value, which is the value returned when the corresponding key is not found.</p></p><p><p>The code is as Follows:</p></p><pre class="prettyprint"><code class="language-java hljs "><span class="hljs-annotation"><span class="hljs-annotation">@Override</span></span><span class="hljs-keyword"><span class="hljs-keyword">protected</span></span> <span class="hljs-keyword"><span class="hljs-keyword">void</span></span> <span class="hljs-title"><span class="hljs-title">Onresume</span></span>(){<span class="hljs-keyword"><span class="hljs-keyword">Super</span></span>. Onresume (); Sharedpreferences sharedpreferences = Getapplicationcontext (). getsharedpreferences (<span class="hljs-string"><span class="hljs-string">"sywyg"</span></span>,<span class="hljs-number"><span class="hljs-number">0</span></span>); String name = Sharedpreferences.getstring (<span class="hljs-string"><span class="hljs-string">"name"</span></span>,<span class="hljs-string"><span class="hljs-string">""</span></span>);<span class="hljs-keyword"><span class="hljs-keyword">Long</span></span>id = Sharedpreferences.getlong (<span class="hljs-string"><span class="hljs-string">"id"</span></span>,<span class="hljs-number"><span class="hljs-number">0</span></span>); Toast.maketext (<span class="hljs-keyword"><span class="hljs-keyword"></span> this</span>,<span class="hljs-string"><span class="hljs-string">"name:"</span></span>+ name +<span class="hljs-string"><span class="hljs-string">", id:"</span></span>+ id,toast.length_long). Show (); Sharedpreferences.editor Editor = Sharedpreferences.edit ();<span class="hljs-comment"><span class="hljs-comment">//clear the contents of the Sharedpreferences file</span></span>Editor.clear (); Editor.commit ();}</code></pre>Results Demo<p><p></p></p><p><p>Novice on the road, a lot of advice.</p></p> <blockquote> <blockquote> <p>Guo Lin First Line<br>Brother Wai Video</p> </blockquote> </blockquote> <p style="font-size:12px;"><p style="font-size:12px;">Copyright Notice: This article for Bo Master original article, without Bo Master permission not Reproduced.</p></p> <p><p>Android's sharedpreferences</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