我的应用程序有一个登录活动,我想"锁定"用户在经过一定次数的尝试后退出。我想将我的int计数器存储在共享偏好中,但我不确定如何设置计时器以便在首选项中恢复。这是我登录的代码。
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){
}
}
}
}
});
答案 0 :(得分:0)
锁定应用,然后创建Handler以等待该时间:
//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.
您的lock_app()
功能可以设置button as disabled,show a Dialog或ProgressBar。
ask_for_login_again()
也可称为unlock_app()
。
答案 1 :(得分:0)
int count = 3;
String attempt;
将此代码放在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);
}
在asynctask
成功时保存您的尝试SaveSharedPreference.set_attempt(Login.this,attempt);
答案 2 :(得分:0)
使用闹钟管理器添加计时器。这样,当用户不在应用程序上时,它仍将倒计时