On one screen I register the password (button register) and on the other screen I need it to be validated for the application to shut down (button off).
I have tried in many ways, but I can not get the database password to do this validation, I can only validate with a default password.
FIRST SCREEN (Where 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
}
}
}
}