Android - Keep user logged in

2019-03-20 22:54发布

问题:

I'm trying to make a login with PHP and MySQLi for Android. What I don't understand is how to keep the user logged in? I saw a simple tutorial where someone used SQLite to safe information but I'm not aware if that is really secure way. How should I save the user information to keep the users logged in?

Thank you.

回答1:

Use the SharedPreferences in android

when your loggedin store the data using

SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0); // 0 - for private mode
Editor editor = pref.edit();
//on the login store the login
editor.putLong("key_name", "long value"); 
editor.commit();

retrieves the data of the key

pref.getString("key_name", "");
                            ^
   default value if the user is not loggedin

clear the data when logout

editor.remove("name");
editor.commit();

Refer this Link for More