<span style= "font-family:arial, Helvetica, Sans-serif; font-size:12px; Background-color:rgb (255, 255, 255); " >sharedpreferences is actually a class that writes data to external memory and reads data. The data exists in XML mode. </span>
Four modes of operation for sharedpreferences data
- context.mode_private
- context.mode_append
- context.mode_world_readable
- context.mode_world_writeable
Context.mode_private: Is the default mode of operation, which means that the file is private and can only be accessed by the app itself, in which the contents of the write overwrite the contents of the original file Context.mode_append: Mode checks whether the file exists, Append content to the file if it exists, or create a new file. Context.mode_world_readable and context.mode_world_writeable are used to control whether other apps have permission to read and write to the file. Mode_world_ Readable: Indicates that the current file can be read by another application. Mode_world_writeable: Indicates that the current file can be written by another application. The following example will be used
Import Android.app.activity;import Android.content.sharedpreferences;import Android.content.sharedpreferences.editor;import Android.os.bundle;import Android.widget.toast;public Class Mainactivity extends Activity { @Override protected void onCreate (Bundle savedinstancestate) { Super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); Sharedpreferences sp =getsharedpreferences ("Bee", mode_private); Bee is the name of the generated XML, Mode_private is private mode. Editor editor= sp.edit (); Get the editor, then add the data editor.putstring ("username", "Eric"); Editor.putstring ("Passws", "lzw213"); Editor.commit (); Submit data //Get Data String result=sp.getstring ("username", "error"); Toast.maketext (this, result, 0). Show (); }}
Remembering user name and password features with sharedpreferences implementation