Remembering user name and password features with sharedpreferences implementation

Source: Internet
Author: User

<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

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.