X失败尝试后的Android Block登录



我有一个带有登录的Android应用程序
失败尝试后,我试图阻止登录X分钟。 如果用户尝试超过y的时间,我想与系统时间进行比较,x分钟用户可以登录如何实现Android的新手,请帮助我

lgn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        uname = user.getText().toString();
        pass = passwrd.getText().toString();
        if (isConnectingToInternet()) {
            if ((passwrd.equals() == "username") || (user.equlas() == "password")) {
                Toast ->success
            }
            if(count >=4){
                // this part i dont know how to 
                //i want to block 30 minute if user tried x attemts, 
                lgnbutton->disble
            }
        }
    }
}

,而不是使用系统的时间来计算x分钟,您可以使用Android的CountdownTimer类。

https://developer.android.com/reference/android/os/countdowntimer.html

y尝试失败后,您可以使用btn.setEnabled(false)禁用该按钮,并且柜台完成后,可以使用btn.setEnabled(true)启用按钮。

,如果您要显示剩余的时间以再次启用该按钮的时间,也可以使用CountdownTimer类(onTick)的方法。 onTick在您提到的每一个时间间隔之后将被调用为CountDownTimer()中的参数。

例如,

new CountDownTimer(<total_time>, <interval>) {
     public void onTick(long millisUntilFinished) {
         //update time left
     }
     public void onFinish() {
         //enable button
     }
  }.start();
import android.content.Context;
import android.content.SharedPreferences;
import android.os.CountDownTimer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Button;
import java.io.IOException;
public class LogInActivity extends AppCompatActivity {
    private Button logInButton;
    private SharedPreferences prefs;
    private long timeLeft;
    private CountDownTimer timer;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_log_in);
        initTasks();
        checkTimer();
    }
    private void initTasks() {
        logInButton = (Button) findViewById(R.id.bt);
        prefs = getSharedPreferences("file", Context.MODE_PRIVATE);
    }
    private void checkTimer() {
        if (prefs.contains("time"))
            setTimer();
        else {
            SharedPreferences.Editor editor = prefs.edit();
            editor.putLong("time", -1L);
            editor.apply();
        }
    }
    private void setTimer() {
        timeLeft = prefs.getLong("time", -1L);
        if (timeLeft != -1L)
            startTimer(timeLeft);
        else
            logInButton.setEnabled(true);
    }
    private void startTimer(long time) {
        logInButton.setEnabled(false);
        timer = new CountDownTimer(time, 1000) {
            @Override
            public void onFinish() {
                logInButton.setEnabled(true);
                saveToPref(-1L);
            }
            @Override
            public void onTick(long millisUntilFinished) {
                //update UI, if required
                timeLeft = millisUntilFinished;
                saveToPref(timeLeft);
            }
        };
    }
    private void saveToPref(long timeLeft){
        SharedPreferences.Editor editor = prefs.edit();
        editor.putLong("time", timeLeft);
        editor.apply();
    }
}

call startTimer(<your_time>)当所有尝试失败时。<your_time>是您希望按钮保持禁用的时间。它以毫秒为单位。

我尚未测试代码,因此可能发生问题。这是满足您要求的基本思想。

时间计数器仅在应用程序运行时起作用。在这种情况下,我将以更艰难的方式计算时间。首先,以毫秒为单位,将当前时间写入共享的首选项,当应用程序再次开始时,从偏好中获得书面毫秒,并计算倒数倒数的间隔。我的报价将是:

        // get saved time if it exist. Do it on app start or activity create
        Long defaultTime = null;
        SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
        Long writtenTime = null;
        try {
            writtenTime = sharedPreferences.getLong("timer", defaultTime);
        } catch (ClassCastException e) {}
        // DO THIS ONLY WHEN BUTTON IS DISABLED
        // count how much time left for countdown
        Long current = Calendar.getInstance().getTime().getTime();
        long interval = (writtenTime == null)? 30000L : current - writtenTime;
        // save new current time
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putLong("timer", current);
        editor.apply();
        // countdown and enable button on finish
        new CountDownTimer(current+interval, interval) {
            public void onTick(long millisUntilFinished) {}
            public void onFinish() {
                btn.setEnabled(true);
            }
        }.start();

最新更新