簡體   English   中英

X嘗試失敗后android阻止登錄

[英]android block login after X failed attempts

我有一個登錄的Android應用程序
y次嘗試失敗后,我試圖阻止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
            }
        }
    }
}

您可以使用android的CountDownTimer類來代替使用系統時間來計算x分鍾。

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

嘗試y次失敗后,可以使用btn.setEnabled(false)禁用按鈕,計數器完成后,可以使用btn.setEnabled(true)啟用按鈕。

如果要顯示按鈕再次啟用的剩余時間,還可以使用CountDownTimer類的方法( onTick )。 在您提到作為CountDownTimer()的參數的每個時間間隔之后,都會調用onTick

例如

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>您的時間<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();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM