Password Validation Android Studio

Asked

Viewed 1,635 times

0

On one screen I register the password (sign up button) and on the other screen I need it to be validated for the application to turn off (turn off button).

I have tried several ways, but I can not find the password of the database to do this validation, I can only validate with a default password.

FIRST SCREEN (Where the password is saved)

//Expecificação
EditText editEmail, editSenha; //componetntes do projeto
SQLiteDatabase db; //DB é o banco de dados

//SharedPreferences


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_cadastro);

    if (getIntent().getBooleanExtra("Exit", false)){
        finish();
    }


    editEmail=(EditText)findViewById(R.id.editEmail);
    editSenha=(EditText)findViewById(R.id.editSenha);

    db=openOrCreateDatabase("CadastroDB", Context.MODE_PRIVATE, null);
    db.execSQL("CREATE TABLE IF NOT EXISTS cadastro (Email VARCHAR, Senha VARCHAR);");

}

//Botão Cadastrar

public void startDesligar (View view) {

    if(isEmailValid(editEmail.getText().toString().trim()) && isPasswordValid(editSenha.getText().toString().trim()))
    {
        db.execSQL("INSERT INTO cadastro VALUES('" + editEmail.getText() + "','" + editSenha.getText() + "');");
        showMessage("Tudo certo!", "Dados Gravados");
        clearText();
        Toast.makeText(getApplicationContext(), "Redirecionando...", Toast.LENGTH_LONG).show();
        Intent Desligar = new Intent(this, Desligar.class);
        startActivity(Desligar);
    } else {
        showMessage("Preencha os campos corretamente!", "E-mail/Senha inválido");
    }
}

//ShowMessage

public void showMessage(String title, String message)
{
    AlertDialog.Builder builder=new AlertDialog.Builder(this);
    builder.setCancelable(true);
    builder.setTitle(title);
    builder.setMessage(message);
    builder.show();
}

//ClearText

public void clearText()
{
    editEmail.setText("");
    editSenha.setText("");
}

private boolean isEmailValid (String email) {
    Pattern pattern;
    Matcher matcher;

    final String EMAIL_PATTERN = (".+@.+\\.[a-z]+");

    pattern = Pattern.compile(EMAIL_PATTERN);
    matcher = pattern.matcher(email);

    return matcher.matches();
}

private boolean isPasswordValid (String password) {

    Pattern pattern;
    Matcher matcher;

    final String PASSWORD_PATTERN = "^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=\\S+$).{4,}$";

    pattern = Pattern.compile(PASSWORD_PATTERN);
    matcher = pattern.matcher(password);

    return matcher.matches();
}

}

SECOND SCREEN (where I use the password to turn off)

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_desligar); // Determina qual layout vai abrir

    senha = (EditText)findViewById(R.id.editSenha); // Senha = EditSenha

    tentativas = (EditText)findViewById(R.id.editTentativas); // Tentativa = editTentativas

    btnir = (Button)findViewById(R.id.button);
}

public void startRecuperacao (View view) {

    Intent Recuperacao = new Intent(this, Recuperacao.class);
    startActivity(Recuperacao);
}

public void btndesligar (View view) {
    if
            (senha.getText().toString().equals("senha digitada")) {
        Intent intent = new Intent(this, CadastroActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.putExtra("Exit", true);
        Toast.makeText(getApplicationContext(), "Desligando...", Toast.LENGTH_SHORT).show();
        startActivity(intent);
        finish();
    } else {
        Toast.makeText(getApplicationContext(), "Senha Incorreta", Toast.LENGTH_SHORT).show();
        tentativas.setVisibility(View.VISIBLE); //Deixar o texto com o número de tentativas
        counter--;
        tentativas.setText(Integer.toString(counter));

        if (counter == 0) {
            btnir.setEnabled(false); //Desabilitar o botão de Desligar
        }
    }

}

}

  • 1

    You are comparing, in the if of the second screen, whether the string typed by the user in the password editText is equal to the string "password typed". You must recover the password you recorded in the bank and compare with it !!

  • But that’s exactly what I want to know how you do, because even you didn’t talk, I created an if to do this validation with the password I manually entered, I want to do this validation with the password that is registered in the database.

1 answer

1


Use the Cursor to perform SELECT type queries more easily on Android Sqlitedatabase. See if you can solve your problem this way

public void btndesligar (View view) {

    String query = "SELECT * FROM cadastro";
    Cursor cursor = db.rawQuery(query, null);

    if(cursor.moveToFirst()){
        do{
            if(senha.getText().toString().equals(cursor.getString(1))){
                Intent intent = new Intent(this, CadastroActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                intent.putExtra("Exit", true);
                Toast.makeText(getApplicationContext(), "Desligando...", Toast.LENGTH_SHORT).show();
                startActivity(intent);
                finish();
            }
        } while(cursor.moveToNext());

        Toast.makeText(getApplicationContext(), "Senha Incorreta", Toast.LENGTH_SHORT).show();
        tentativas.setVisibility(View.VISIBLE); //Deixar o texto com o número de tentativas
        counter--;
        tentativas.setText(Integer.toString(counter));
        if (counter == 0) {
                btnir.setEnabled(false); //Desabilitar o botão de Desligar
            }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.