In my projects I create an initial Activity with start name, in it I check if there is something saved in the cellular memory to do the automatic login, note my example:
public class start extends AppCompatActivity {
String login, senha;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.autenticacao_start);
read(new View(this));
}
public void read(View view) {
try {
FileInputStream fin = openFileInput("nome_do_save");
int c;
String temp = "";
while ((c = fin.read()) != -1) {
temp = temp + Character.toString((char) c);
}
login = temp.split(";separador;")[0];
senha = temp.split(";separador;")[1];
logar();
} catch (Exception e) {
// TODO Auto-generated catch block
//Em caso de erro ou não existir eu movo o cliente para tela de login
Intent it = new Intent(start.this, login.class);
startActivity(it);
finish();
e.printStackTrace();
}
}
public void logar() {
//Sua lógica de login
}
}
This is how to read, now to save on the login screen I use:
public class login extends AppCompatActivity {
EditText editEmail1, editSenha1;
Button btnLogar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.autenticacao_login);
editEmail1 = (EditText)findViewById(R.id.editEmail1);
editSenha1 = (EditText)findViewById(R.id.editSenha1);
btnLogar = (Button)findViewById(R.id.btnLogar);
btnLogar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//chama método logar, se este retornar retorna que existe o usuário então eu chamo o save();
}
});
}
public void save(View view){
String dados;
dados = editEmail1.getText().toString() + ";wotonbest;" + editSenha1.getText().toString();
try {
FileOutputStream fOut = openFileOutput("nome_do_save",MODE_PRIVATE);
fOut.write(dados.getBytes());
fOut.close();
Toast.makeText(getApplicationContext(), "Logado com sucesso!", Toast.LENGTH_SHORT).show();
Intent it = new Intent(login.this, Main.class);
startActivity(it);
finish();
} catch (Exception e) {
// TODO Auto-generated catch block
//Toast.makeText(getApplicationContext(), "ERRO", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
}