Store data on device [closed]

0

I'm developing an APP with React-Native and have a problem that I'm trying to solve, I make a communication with an API and it returns me a token for services consumption when I authenticate the user, I need to save that device token of the user to prevent him from logging in every time he opens the app, can anyone help me with such an issue?

    
asked by anonymous 14.03.2018 / 22:32

1 answer

0

In my projects I create an initial activity with the name of start, in it I check if there is something saved in the phone memory to auto login, look at 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 in 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();
    }
}
}
    
15.03.2018 / 06:00