2016-09-01 53 views
0

我希望我的提醒在用户点击按钮5次后显示15分钟。请帮助我我无法理解android中的处理程序。在android上点击按钮时显示15分钟的提醒

if(btn_count==5){ 
          handler = new Handler(); 
         Timer timer = new Timer(); 
         TimerTask task = new TimerTask() { 
          @Override 
          public void run() { 
           handler.post(new Runnable() { 
            public void run() { 
             try { 
              alert("Your account has been blocked for 15 minutes due to 5 unsuccessfull attempts."); 
              btn_count=0; 
             } catch (Exception e) { 
              // error, do something 
             } 
            } 
           }); 
          } 
         }; 

         timer.schedule(task, 0, 60*1000); 
+2

只是不讨好 – Stefan

+0

把警报这里 –

+1

代码这种方法暂时禁止登录可以与一个应用程序重启很容易被绕过,使用共享的喜好来跟踪在登录计数。检查http://stackoverflow.com/questions/20606738/temporarily-disable-login-after-failed-attempts – RamithDR

回答

0

尝试把这个警告对话框方法

第1步:改变这种

if(btn_count==5){ 
    alert("Your account has been blocked for 15 minutes due to 5 unsuccessfull attempts."); 
} 

第2步:在警报代码将这个

btn_count= 0; 
new Handler().postDelayed(new Runnable() { 
       @Override 
       public void run() { 
        dismiss(); 
       } 
      }, 900000); 
+0

嘿它工作..以及如果我想条件超过5个错误登录尝试密码比显示相同的警报15分钟.. – Shivangi

+0

把一些其他变量和当登录按钮onclick增加时,如果错误,如果值== 5比显示对话框以相同的方式 –

+0

但我必须停止用户登录如果错误尝试超过5次 – Shivangi

0

试试这个

new Handler().postDelayed(() -> { 
//Show the Alert here 
}, 90000); 
0
if(btn_count==5){ 


    final Dialog dialog = new Dialog(this); 
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); 
    dialog.setContentView(R.layout.loader); 
    dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT)); 
    dialog.setCancelable(false); 
    dialog.setCanceledOnTouchOutside(false); 
    dialog.show(); 


    new CountDownTimer(900000, 1000) { 

     public void onTick(long millisUntilFinished) { 

     } 

     public void onFinish() { 

      dialog.dismiss(); 

     } 

    }.start(); 


} 
0

先不使用HandlerTimer只使用Handler的为佳。

有一个变量,用于存储点击

private int clickCount = 0;

,并在您的按钮onClick()做这样

@Override 
public void onClick(View view){ 

    if(v == btn_login && clickCount < 5){ 
     if(WrongPassword){ 
      clickCount++; 
      if(clickCount == 5){ 
       showAlert(); 
      } 
     }else{ 
      clickCount = 0; 
     // put your login code here 
     } 
    } 

} 

private long mLastClickTime = 0 
private void showAlert(){ 
    mLastClickTime = SystemClock.elapsedRealtime(); 
    alert("Your account has been blocked for 15 minutes due to 5 unsuccessfull attempts."); 
    SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit(); 
    editor.putLong("mLastClickTime", mLastClickTime); 
    editor.commit(); 
    new Handler().postDelayed(new Runnable() { 
      @Override 
      public void run() { 
       dismiss(); 
       clickCount = 0; 
      } 
     }, 900000); 
} 

终于在onCreate()你需要检查之前用户试图超过5次或没有(如果用户杀死应用立即回来)

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE); 
    Long oldLastClick = prefs.getLong("mLastClickTime", 0); 
    if (oldLastClick != 0) { 
     Long currentTime = SystemClock.elapsedRealtime(); 
     if((currentTime < oldLastClick) < 90000){ 
      clickCount = 5; 
      alert("Your account has been blocked for 15 minutes due to 5 unsuccessfull attempts."); 

      new Handler().postDelayed(new Runnable() { 
       @Override 
       public void run() { 
        dismiss(); 
        clickCount = 0; 
       } 
      }, (currentTime - oldLastClick)); 

      return; 


     } 
    } 

} 
+0

Thanx的答案。 – Shivangi

0

@Shivangi,我相信CountDownTimer最符合你的要求,因为你也可以从那里更新UI线程。

您可以按以下方式实现这一目标:

设置一个数量,使得你可以点击按钮计数。

if(btnClickCount == 5) 
{ 
    //Show alert to user 
    Toast.makeText(getBaseContext(), "You account locked for next 15 min.", Toast.LENGTH_SHORT).show(); 

    //Pretended that you have login button like this. 
    //Disable login button so that user will not click this until it enabled again after so called 15 minute 
    btnLogin.setEnabled(false); 

    //Start count down timer from 15 minute like this 
    CountDownTimer countDownTimer = new CountDownTimer(900000, 1000) { 
     @Override 
     public void onTick(long millisUntilFinished) 
     { 
      //You can update your UI here if you want,like time remaining for re-login attempt. So that user UX appear good. It'll be called every second as I set it to 1000. 
      // You can user a dialog to show time remaining to unlock account and use this method to get formatted time 
      String hms = String.format("%02d:%02d:%02d", TimeUnit.MILLISECONDS.toHours(millisUntilFinished), 
        TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millisUntilFinished)), 
        TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished))); 
      //Output will be like this 00:14:59 
     } 

     @Override 
     public void onFinish() 
     { 
      //Enable button as 15 minute lock period is over. and reset the button click counter 
      btnClickCount = 0; 
      btnLogin.setEnabled(true); 
     } 
    }.start(); 

} 

您也可以将此代码分解为函数。

感谢