Android login and password "cache"

4

Hello, how to do an automatic login?

ex: I made an android application with login, password and webservice, when the user logs I send the information and return true or false. How do I store on the phone the user and password informed for the next accesses do not ask the same?

    
asked by anonymous 08.09.2015 / 16:41

1 answer

4

You can user SharedPreferences to save the information, so the second time the user accesses your application, you verify that this information is populated and automatically log in.

Example to save the information using SharedPreferences :

public static final String NOME_PREFERENCE = "INFORMACOES_LOGIN_AUTOMATICO";
SharedPreferences.Editor editor = getSharedPreferences(NOME_PREFERENCE, MODE_PRIVATE).edit();

 editor.putString("login", "usuario01");
 editor.putString("senha", "1234");
 editor.commit();

And for you to recover this saved information:

SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE); 
String login= prefs.getString("login", null);
String senha= prefs.getString("senha", null);
if (login!= null) {
   // existe configuração salvar
} else {
  // não existe configuração salvar
}
    
12.09.2015 / 21:52