Android Development Road 11 (store data with Sharedpreferences)
Last Update:2016-03-19
Source: Internet
Author: User
<span id="Label3"></p><p><p><span style="font-size: 15px;">The Android <span style="font-family: 宋体;">platform provides us with a</span> <span style="font-family: Consolas;">sharedpreferences</span> <span style="font-family: 宋体;">class that actually</span> <span style="font-family: Consolas;">sharedpreferences</span> <span style="font-family: 宋体;">is dealing with a</span> <span style="font-family: Consolas;">Key-value</span> <span style="font-family: 宋体;">(key-value pair), which is</span></span></p></p><p><p><span style="font-size: 15px;"></span></p></p><p><p><span style="font-size: 15px;">A lightweight storage class that is ideal for saving software configuration parameters and user preferences, such as remembering password functions at login Time. Use</span></p></p><p><p><span style="font-size: 15px;"></span></p></p><p><p><span style="font-size: 15px;">Sharedpreferences <span style="font-family: 宋体;">saves the data, actually</span> holds the data with the <span style="font-family: Consolas;">XML</span> <span style="font-family: 宋体;">file, The file is stored in the</span> <span style="font-family: Consolas;">/data/data/<package name>/shared </span> <span style="font-family: 宋体;">under the _prefs directory</span></span></p></p><p><p><span style="font-size: 15px;"></span></p></p><p><p><span style="font-size: 15px;">:</span></p></p><p><p><span style="font-size: 15px;">1. There <span style="font-family: 宋体;">are two ways to</span> <span style="font-family: 宋体;">get</span> <span style="font-family: Consolas;">sharedpreferences</span> objects <span style="font-family: Consolas;">:</span></span></p></p><p><p><span style="font-size: 15px;">① calling the <span style="font-family: Consolas;">getsharedpreferences ()</span> <span style="font-family: 宋体;">method</span> <span style="font-family: 宋体;">of the</span> <span style="font-family: Consolas;">Context</span> Object</span></p></p><p><p><span style="font-size: 15px;">② calling <span style="font-family: Consolas;"></span> <span style="font-family: 宋体;">the Activity object's</span> <span style="font-family: Consolas;">getpreferences ()</span> <span style="font-family: 宋体;">method</span></span></p></p><p><p><span style="font-size: 15px;">The difference between the two ways <span style="font-family: Consolas;">:</span></span></p></p><p><p><span style="font-size: 15px;">Called by the <span style="font-family: Consolas;">getsharedpreferences ()</span> method of the <span style="font-family: Consolas;">Context</span> <span style="font-family: 宋体;">object</span> to <span style="font-family: 宋体;">obtain the</span> <span style="font-family: Consolas;">sharedpreferences</span> <span style="font-family: 宋体;">objects can be shared by other components under the same application</span> <span style="font-family: Consolas;">.</span></span></p></p><p><p><span style="font-size: 15px;">The <span style="font-family: Consolas;">sharedpreferences</span> object <span style="font-family: 宋体;">obtained by</span> invoking the <span style="font-family: Consolas;">getpreferences ()</span> method of the <span style="font-family: Consolas;">Activity</span> <span style="font-family: 宋体;">Object</span> <span style="font-family: 宋体;">can only be </span> <span style="font-family: Consolas;"></span> <span style="font-family: 宋体;">use in</span> Activity <span style="font-family: Consolas;">.</span></span></p></p><p><p><span style="font-size: 15px;"><span style="font-family: 宋体;">Four modes of operation in</span> 2.SharedPreferences <span style="font-family: Consolas;">:</span></span></p></p><p><p><span style="font-size: 15px;">Context.mode_private<span style="font-family: 宋体;">: The default mode of Operation</span> <span style="font-family: Consolas;"></span> , which means that <span style="font-family: 宋体;">the file is private</span> <span style="font-family: Consolas;">and</span> <span style="font-family: 宋体;">can only be accessed</span> <span style="font-family: Consolas;"></span> <span style="font-family: 宋体;">by the app Itself. In this mode</span> <span style="font-family: Consolas;">,</span> the content that is <span style="font-family: 宋体;">written overwrites the original file</span></span></p></p><p><p><span style="font-size: 15px;"></span></p></p><p><p><span style="font-size: 15px;">of the content</span></p></p><p><p><span style="font-size: 15px;">Context.mode_append<span style="font-family: 宋体;">: Mode checks whether a file exists</span> <span style="font-family: Consolas;">,</span> <span style="font-family: 宋体;">appends content</span> <span style="font-family: Consolas;">to the file,</span> <span style="font-family: 宋体;">or creates a new file</span> <span style="font-family: Consolas;">.</span></span></p></p><p><p><span style="font-size: 15px;">Context.mode_world_readable <span style="font-family: 宋体;">and</span> <span style="font-family: Consolas;">context.mode_world_writeable</span> are <span style="font-family: 宋体;">used to control whether other apps have permission to read and write to the file</span> <span style="font-family: Consolas;">.</span></span></p></p><p><p><span style="font-size: 15px;">Mode_world_readable<span style="font-family: 宋体;">: Indicates that the current file can be read by another application</span> <span style="font-family: Consolas;">.</span></span></p></p><p><p><span style="font-size: 15px;">Mode_world_writeable<span style="font-family: 宋体;">: Indicates that the current file can be written by another application</span> <span style="font-family: Consolas;">.</span></span></p></p><p><p><span style="font-size: 15px;"></span></p></p><p><p><span style="font-size: 15px;"><span style="font-family: 宋体;">Examples of applications for 3.SharedPreferences Classes:</span></span></p></p><p><p><span style="font-size: 15px;">① creating a layout file:<span style="font-family: Consolas;">activity_main.xml</span></span></p></p><p><p><span style="font-size: 15px;"><linearlayout xmlns:android= "http://schemas.android.com/apk/res/android"</span></p></p><p><p><span style="font-size: 15px;">Android:layout_width= "match_parent"</span></p></p><p><p><span style="font-size: 15px;">android:layout_height= "match_parent"</span></p></p><p><p><span style="font-size: 15px;">android:orientation= "vertical" ></span></p></p><p><p><span style="font-size: 15px;"></span></p></p><p><p><span style="font-size: 15px;"><textview</span></p></p><p><p><span style="font-size: 15px;">Android:layout_width= "wrap_content"</span></p></p><p><p><span style="font-size: 15px;">android:layout_height= "wrap_content"</span></p></p><p><p><span style="font-size: 15px;">android:text= "@string/name"/></span></p></p><p><p><span style="font-size: 15px;"><edittext</span></p></p><p><p><span style="font-size: 15px;">Android:id= "@+id/name"</span></p></p><p><p><span style="font-size: 15px;">Android:layout_width= "match_parent"</span></p></p><p><p><span style="font-size: 15px;">android:layout_height= "wrap_content"/></span></p></p><p><p><span style="font-size: 15px;"><textview</span></p></p><p><p><span style="font-size: 15px;">Android:layout_width= "wrap_content"</span></p></p><p><p><span style="font-size: 15px;">android:layout_height= "wrap_content"</span></p></p><p><p><span style="font-size: 15px;">android:text= "@string/age"/></span></p></p><p><p><span style="font-size: 15px;"><edittext</span></p></p><p><p><span style="font-size: 15px;">Android:id= "@+id/age"</span></p></p><p><p><span style="font-size: 15px;">Android:layout_width= "match_parent"</span></p></p><p><p><span style="font-size: 15px;">android:layout_height= "wrap_content"</span></p></p><p><p><span style="font-size: 15px;">Android:numeric= "integer"/></span></p></p><p><p><span style="font-size: 15px;"><!--android:onclick is used to specify a method name, the button is clicked and the method is Executed.</span></p></p><p><p><span style="font-size: 15px;"><button</span></p></p><p><p><span style="font-size: 15px;">Android:id= "@+id/button"</span></p></p><p><p><span style="font-size: 15px;">Android:layout_width= "wrap_content"</span></p></p><p><p><span style="font-size: 15px;">android:layout_height= "wrap_content"</span></p></p><p><p><span style="font-size: 15px;">android:text= "@string/button"</span></p></p><p><p><span style="font-size: 15px;">android:onclick= "save"/></span></p></p><p><p><span style="font-size: 15px;"></span></p></p><p><p><span style="font-size: 15px;"></LinearLayout></span></p></p><p><p><span style="font-size: 15px;"></span></p></p><p><p><span style="font-size: 15px;">② Creating a Business class <span style="font-family: Consolas;">Preferenceservice.java</span></span></p></p><p><p><span style="font-size: 15px;">public class Preferenceservice {</span></p></p><p><p><span style="font-size: 15px;">Private context context;</span></p></p><p><p><span style="font-size: 15px;"></span></p></p><p><p><span style="font-size: 15px;">Public Preferenceservice (context Context) {</span></p></p><p><p><span style="font-size: 15px;">This.context = context;</span></p></p><p><p><span style="font-size: 15px;">}</span></p></p><p><p><span style="font-size: 15px;"></span></p></p><p><p><span style="font-size: 15px;">Saving configuration parameters</span></p></p><p><p><span style="font-size: 15px;">public void Save (String Name,integer Age) {</span></p></p><p><p><span style="font-size: 15px;">Sharedpreferences sp=context.getsharedpreferences ("testsp", context.mode_private);</span></p></p><p><p><span style="font-size: 15px;">Editor Editor=sp.edit ();</span></p></p><p><p><span style="font-size: 15px;">Editor.putstring ("name", name);</span></p></p><p><p><span style="font-size: 15px;">Editor.putint ("age", age);</span></p></p><p><p><span style="font-size: 15px;">Files that are submitted to the data</span></p></p><p><p><span style="font-size: 15px;">Editor.commit ();</span></p></p><p><p><span style="font-size: 15px;">}</span></p></p><p><p><span style="font-size: 15px;">Get configuration parameters</span></p></p><p><p><span style="font-size: 15px;">Public map<string, string> getpreference () {</span></p></p><p><p><span style="font-size: 15px;">Create a map collection to hold the data we get from sharedpreference</span></p></p><p><p><span style="font-size: 15px;">map<string, string> params=new hashmap<string, string> ();</span></p></p><p><p><span style="font-size: 15px;">Sharedpreferences sp=context.getsharedpreferences ("testsp", context.mode_private);</span></p></p><p><p><span style="font-size: 15px;">The first parameter in the GetString ("name", "") method of the Sharedpreferences class is the parameter name, and the first parameter is the default value of the parameter</span></p></p><p><p><span style="font-size: 15px;">Params.put ("name", sp.getstring ("name", "" "));</span></p></p><p><p><span style="font-size: 15px;">Params.put ("age", string.valueof (sp.getint ("age", 0));</span></p></p><p><p><span style="font-size: 15px;">Return params;</span></p></p><p><p><span style="font-size: 15px;"></span></p></p><p><p><span style="font-size: 15px;">}</span></p></p><p><p><span style="font-size: 15px;"></span></p></p><p><p><span style="font-size: 15px;">}</span></p></p><p><p><span style="font-size: 15px;"></span></p></p><p><p><span style="font-size: 15px;">③ Creating a program entry <span style="font-family: Consolas;">Mainactivity.java</span></span></p></p><p><p><span style="font-size: 15px;">public class Mainactivity extends Activity {</span></p></p><p><p><span style="font-size: 15px;">Private EditText nametext;</span></p></p><p><p><span style="font-size: 15px;">Private EditText agetext;</span></p></p><p><p><span style="font-size: 15px;">Private Preferenceservice service;</span></p></p><p><p><span style="font-size: 15px;">@Override</span></p></p><p><p><span style="font-size: 15px;">protected void OnCreate (Bundle Savedinstancestate) {</span></p></p><p><p><span style="font-size: 15px;">Super.oncreate (savedinstancestate);</span></p></p><p><p><span style="font-size: 15px;">Setcontentview (r.layout.activity_main);</span></p></p><p><p><span style="font-size: 15px;">nametext= (EditText) Findviewbyid (r.id.name);</span></p></p><p><p><span style="font-size: 15px;">agetext= (EditText) Findviewbyid (r.id.age);</span></p></p><p><p><span style="font-size: 15px;">Service =new Preferenceservice (this);</span></p></p><p><p><span style="font-size: 15px;">map<string, string> params=service.getpreference ();</span></p></p><p><p><span style="font-size: 15px;">Nametext.settext (params.get ("name"));</span></p></p><p><p><span style="font-size: 15px;">Agetext.settext (params.get ("age"));</span></p></p><p><p><span style="font-size: 15px;"></span></p></p><p><p><span style="font-size: 15px;">}</span></p></p><p><p><span style="font-size: 15px;">/**</span></p></p><p><p><span style="font-size: 15px;">* The Save Method Requires:</span></p></p><p><p><span style="font-size: 15px;">* Parameters must be of view type</span></p></p><p><p><span style="font-size: 15px;">* And no return value</span></p></p><p><p><span style="font-size: 15px;">*/</span></p></p><p><p><span style="font-size: 15px;">public void Save (View V) {</span></p></p><p><p><span style="font-size: 15px;">String name=nametext.gettext (). toString ();</span></p></p><p><p><span style="font-size: 15px;">String age=agetext.gettext (). toString ();</span></p></p><p><p><span style="font-size: 15px;">Service.save (name,integer.valueof (age));</span></p></p><p><p><span style="font-size: 15px;">Toast.maketext (getapplicationcontext (), "saved successfully", toast.length_long). show ();</span></p></p><p><p><span style="font-size: 15px;">}</span></p></p><p><p><span style="font-size: 15px;">}</span></p></p><p><p><span style="font-size: 15px;"></span></p></p><p><p><span style="font-size: 15px;"></span></p></p><p><p><span style="font-size: 15px;"></span></p></p><p><p> Android Development Road 11 (store data with sharedpreferences) </p> </p></span>