0

I have a Log In activity for my app and I would like to "lock" the user out after a certain number of attempts. I would like to store my int counter in the sharedpreferences but I'm unsure as to how to set a timer for it to restore in the preferences. Here is my code for the log in.

login_Button.setOnClickListener(new View.OnClickListener(){


        @Override
        public void onClick(View v) {

            int uid = 1155;
            String pass = "pass";

            SharedPreferences userDetails = getSharedPreferences(User_File, MODE_PRIVATE);
            SharedPreferences.Editor edit = userDetails.edit();

            edit.putInt("userID", uid);
            edit.putString("password", pass);
            edit.commit();


            if((etUserID.getText().toString().equals(""))){
                Toast.makeText(getApplicationContext(),"Please enter a User ID", Toast.LENGTH_LONG).show();
            }else if (etPassword.getText().toString().equals("")){
                Toast.makeText(getApplicationContext(),"Please enter a password", Toast.LENGTH_LONG).show();
            }else{
                String user_id = etUserID.getText().toString();
                int user_id2 = Integer.parseInt(user_id);
                String user_password = etPassword.getText().toString();

                int userID = userDetails.getInt("userID", 1);
                String password = userDetails.getString("password", "no name");

                if (userID == user_id2 && password.equals(user_password)){
                    startActivity(new Intent(LogOn.this,CrimeMap.class));
                }else{
                    counter = counter - 1;
                    Toast.makeText(getApplicationContext(), "You have" + counter + "attempts remaining", Toast.LENGTH_LONG).show();

                    if(counter == 0){

                    }
                }
            }



        }
    }); 
F0xcr4f7
  • 61
  • 1
  • 1
  • 10

3 Answers3

0

Lock the app and then create a Handler to wait up that time:

//Run this code after n unsuccessful attempts

lock_app();

final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
    @Override
    public void run() {
        reset_counter_in_preferences();
        ask_for_login_again();
    }
}, 30000); //Time (in ms) that the app will remain locked for.

Your lock_app() function could set the button as disabled, show a Dialog or a ProgressBar.

The ask_for_login_again() might as well be called unlock_app().

Community
  • 1
  • 1
Evin1_
  • 12,292
  • 9
  • 45
  • 47
0
int count = 3;
String attempt;

put this code in the failure section of the asynctask

   count--;
             your_textview.setText(Integer.toString(count));
             attempt = your_textview.getText().toString();

                if (count == 0) {
                    Toast.makeText(Login.this,"something went wrong",Toast.LENGTH_SHORT).show();
                    btn_submit.setEnabled(false);
                }

save your attempts at the time of success of asynctask

SaveSharedPreference.set_attempt(Login.this,attempt);
Chaudhary Amar
  • 836
  • 8
  • 20
0

Use an alarmmanager to add timer. This way when the user is not on the app it will still countdown

Justin Yang
  • 41
  • 1
  • 5