Java 尝试X次失败后android阻止登录
我有一个登录名为Java 尝试X次失败后android阻止登录,java,android,login,Java,Android,Login,我有一个登录名为 在y次尝试失败后,我尝试阻止登录x分钟。 如果用户尝试超过y次,我想与系统时间进行比较,并且在x分钟后用户可以登录,我如何才能实现我是android新手,请帮助我 lgn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { uname = user.getText().toString(); pass = p
在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的倒计时类,而不是使用系统的时间来计算x分钟 尝试失败后,可以使用
btn.setEnabled(false)
禁用按钮,计数器完成后,可以使用btn.setEnabled(true)
启用按钮
如果要显示按钮再次启用的剩余时间,也可以使用倒计时类(onTick
)方法。
onTick
将在您在CountDownTimer()
中提到的每个时间间隔后调用
例如
new CountDownTimer(<total_time>, <interval>) {
public void onTick(long millisUntilFinished) {
//update time left
}
public void onFinish() {
//enable button
}
}.start();
新的倒计时(,){
公共void onTick(长毫秒未完成){
//剩余更新时间
}
公共无效onFinish(){
//启用按钮
}
}.start();
在所有尝试失败时调用startTimer()
是您希望按钮保持禁用状态的时间。以毫秒为单位
我没有测试代码,因此可能会出现问题。这是满足您需求的基本思想。时间计数器仅在应用程序运行时工作。在这种情况下,我将以更难的方式计算时间。首先获取当前时间(以毫秒为单位),将它们写入共享首选项,当应用程序再次启动时,从首选项中获取写入的毫秒,并计算剩余的倒计时间隔。我的报价是:
// 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();
到目前为止您尝试了什么?
btnLogin.setEnabled(false)代码>很简单。@IntelliJ Amiya我不知道如何与系统时间协调,@这是我知道这是最后一步,使用countdowntimer。如果我使用conter timer,当我退出应用程序时,coutertimer也将停止。如果要取消计时器,可以在活动的onDestroy()中调用同一类的cancel()方法。例如,timer=new CountDownTimer(){…}timer.cancel()我不想取消,当我关闭应用程序时,它将自动停止,如果我设置30分钟,如果我在5分钟后关闭应用程序并重新打开,计时器将重新插入,因此没有用,我想压缩系统时间如果你不想在退出应用程序后停止计时器,您可以保留共享首选项中的剩余时间,当应用程序重新启动时,您可以从首选项中获取剩余时间,并可以使用该时间重新启动计时器。然而,若用户清除了appThankyou的数据,这将不起作用。我尝试过一次,它在defaultTime和WriteTime上显示空指针异常。。我添加了我的代码请帮助我感谢您的回复,我尝试了我未能实现我将共享请帮助我查找错误替换为“b1=view.findViewById(R.id.Button);”替换为“b1=view.findViewById(R.id.Button);”。您正在访问“b1.setEnabled()”中的全局变量,该变量为空,因为您使用了局部变量。在这种情况下,避免使用局部变量。逻辑实现中还有一个错误。您正在单击按钮时调用checkTimer()。它应该在initTasks()之后调用。当所有尝试都失败时,使用startTimer()禁用按钮并启动计时器,如我在回答中所述。在哪里调用按钮单击?在按钮单击时执行登录功能。维护计数器以计算失败尝试的次数。如果数量超过所需限制,请调用startTimer()。
// 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();