Use SharedPreferences to remember the user name and password.

Source: Internet
Author: User

Use SharedPreferences to remember the user name and password.

<Span style = "font-family: Arial, Helvetica, sans-serif; font-size: 12px; background-color: rgb (255,255,255 ); "> SharedPreferences is actually a class in which the operating program writes data to external storage and reads data. Data exists in XML format. </Span>

SharedPreferences data operation modes
  • Context. MODE_PRIVATE
  • Context. MODE_APPEND
  • Context. MODE_WORLD_READABLE
  • Context. MODE_WORLD_WRITEABLE
Context. MODE_PRIVATE: the default operation mode. This mode indicates that the file is private data and can only be accessed by the application itself. In this mode, the written content overwrites the Context of the original file. MODE_APPEND: the mode checks whether the file exists and appends content to the file if it exists. Otherwise, a new file is created. context. MODE_WORLD_READABLE and Context. MODE_WORLD_WRITEABLE is used to control whether other applications have the permission to read and write the file. MODE_WORLD_READABLE: indicates that the current file can be read by other applications. MODE_WORLD_WRITEABLE: indicates that the current file can be written by other applications.




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 indicates the name of the generated xml, and MODE_PRIVATE indicates the private mode. Editor editor = sp. edit (); // get the editor, and then add the data editor. putString ("username", "eric"); editor. putString ("passws", "lzw213"); editor. commit (); // submit data // obtain data String result = sp. getString ("username", "error"); Toast. makeText (this, result, 0 ). show ();}}


Related Article

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.