Android saves the user name and password

Source: Internet
Author: User

Whether we are developing a project or using other projects, we have a user login function. To make the user experience better, we usually create a function called "Save users, in this way, the user will not re-enter the user name and password when using the program next time. Here, I use three methods to store the user name and password.

1. Common txt text Storage

2. Store the properties File

3. Use the SharedPreferences tool to store

First:

/*** Business Method for saving the user name and password ** @ param username * @ param password * @ return */public static boolean saveUserInfo (String username, String password) {try {// use the absolute path File = new file ("data/com. example. android_file_handler/info.txt "); // creates the output stream object FileOutputStream fos = new FileOutputStream (file); // writes information fos to the file. write (username + "#" + password ). getBytes (); // close the output stream object fos. close (); return true;} catch (Exception e) {throw new RuntimeException ();}}

The path written here is the absolute path of the current project, which is flawed. For example, if you change the project path, the path here will fail to be obtained, therefore, Android provides a context method to obtain the path of the current project.

Public static boolean saveUserInfo (Context context, String username, String password) {try {// obtain the path File file = new File (context. getFilesDir (), "userinfo.txt"); // create the output stream object FileOutputStream fos = new FileOutputStream (file); // write information fos to the file. write (username + "#" + password ). getBytes (); // close the output stream object fos. close (); return true;} catch (Exception e) {throw new RuntimeException ();}}

The above two methods both store the user name and password, and the next step is to get the user name and password

/*** Get the information of a common txt file ** @ param context * @ return */public static Map
 
  
GetTxtFileInfo (Context context) {try {// Create FIle object File = new file (context. getFilesDir (), "userinfo.txt"); // create a FileInputStream object, FileInputStream, and new FileInputStream (file ); // create the BufferedReader object BufferedReader br = new BufferedReader (new InputStreamReader (FCM); // obtain the content in the file String content = br. readLine (); // creates a Map set
  
   
Map = new HashMap
   
    
(); // Used to save the information # split the content String [] contents = content. split ("#"); // save it to map in the map set. put ("username", contents [0]); map. put ("password", contents [1]); // close the stream object. close (); br. close (); return map;} catch (Exception e) {e. printStackTrace (); return null ;}}
   
  
 

Here, I encapsulate the obtained content into the Map set. In fact, using common txt text to store user names and passwords is flawed, here, I use "#" to separate the user name and password. If the user's character in the password contains the special symbol "#", then at the end of obtaining the password, therefore, the method in dier can solve this problem.

Second:

Use the property file to save the user name and password

/*** Use the property file to save user information ** @ param context Context * @ param username * @ param password * @ return */public static boolean saveProUserInfo (context, string username, String password) {try {// obtain the path File = new file (context. getFilesDir (), "info. properties "); // create the output stream object FileOutputStream fos = new FileOutputStream (file); // create the property file object Properties pro = new Properties (); // set the user name or password pro. setProperty ("username", username); pro. setProperty ("password", password); // save the file pro. store (fos, "info. properties "); // close the output stream object fos. close (); return true;} catch (Exception e) {throw new RuntimeException ();}}

Read attribute files

/*** Return property file object ** @ param context Context * @ return */public static Properties getProObject (context) {try {// Create File object File = new file (context. getFilesDir (), "info. properties "); // create the FileIutputStream object FileInputStream FD = new FileInputStream (file); // create the property object Properties pro = new Properties (); // load the file pro. load (fisms); // close the input stream object. close (); return pro;} catch (Exception e) {e. printStackTrace (); return null ;}}


You can call it in the main method.

// Obtain the property file object Properties pro = LoginService. readSDCard (this); // obtain the username or password if (null! = Pro) {String username = pro. getProperty ("username"); String password = pro. getProperty ("password"); // if the obtained user name or password is not empty, set it to the text box if (! TextUtils. isEmpty (username )&&! TextUtils. isEmpty (password) {// set the username etUsername. setText (username); // set the password etPassword. setText (password );}}

Third:

Use SharedPreferences to save the user name and password

/*** Use SharedPreferences to save user logon information * @ param context * @ param username * @ param password */public static void saveLoginInfo (Context context, String username, String password) {// obtain the SharedPreferences object SharedPreferences sharedPre = context. getSharedPreferences ("config", context. MODE_PRIVATE); // obtain the Editor object Editor = sharedPre. edit (); // set the parameter editor. putString ("username", username); editor. putString ("password", password); // submit the editor. commit ();}


Read in the main method:

SharedPreferences sharedPre=getSharedPreferences("config", MODE_PRIVATE);String username=sharedPre.getString("username", "");String password=sharedPre.getString("password", "");


The common txt files and attribute files are saved to the internal storage device. You can also save them to SDCard and use the Environment class provided by Android to obtain the external storage device.

File file=new File(Environment.getExternalStorageDirectory(),"info.properties");

If you want to use the absolute path

File file = new File("/sdcard/info.properties");

The last step is to add permissions. Because obtaining external storage devices is secure, you need to add related permissions.

 

You can use these methods to save the user name and password. If there are other methods, I think these methods should be enough.





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.