所以我创建了一个简单的登录活动。当用户未能登录3次时,登录按钮将被禁用一段时间。
我的困难是当我关闭应用程序并再次打开它时按钮被启用。如何解决这个问题?
这是我的代码:
public class LoginControl extends Activity {
private DBControl db = new DBControl(this);
int counter = 2;
Button login = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.loginview);
Button register = (Button) findViewById(R.id.btnCreateA);
login = (Button) findViewById(R.id.btnLogin);
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
EditText a = (EditText) findViewById(R.id.etUser);
EditText b = (EditText) findViewById(R.id.etPassword);
String user = a.getText().toString();
String pass = b.getText().toString();
String confirm = db.getUserPass(user);
if (user.equals("") || pass.equals("")) {
Toast passed = Toast.makeText(LoginControl.this, "Please input required fields.", Toast.LENGTH_LONG);
passed.show();
} else if (pass.equals(confirm)) {
Toast passed = Toast.makeText(LoginControl.this, "Sucess!", Toast.LENGTH_LONG);
passed.show();
Intent intent = new Intent(LoginControl.this, HomeControl.class).putExtra("Music", false);
startActivity(intent);
finish();
} else if (counter == 0)
// Disable button after 3 failed attempts
{
login.setEnabled(false);
Toast alert = Toast.makeText(LoginControl.this, "Login Disabled for 5 mins", Toast.LENGTH_LONG);
alert.show();
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
login.setEnabled(true);
counter = 2;
}
}, 30000);
} else {
Toast passed = Toast.makeText(LoginControl.this, "Username or password don't match!", Toast.LENGTH_LONG);
counter--;
passed.show();
}
} catch (Exception e) {
Toast passed = Toast.makeText(LoginControl.this, e.toString(), Toast.LENGTH_LONG);
passed.show();
}
}
});
}
}
答案 0 :(得分:2)
要获取此信息,即使重新启动应用程序,您也需要节省再次启用该按钮的时间。当您的应用启动时,您可以打开此信息并检查它是在此时间之前还是之后。
有几种方法,如何在android上存储信息:
对于您的问题,我建议您使用共享首选项。
答案 1 :(得分:0)
使用共享偏好设置,在login.setEnabled(false)
之后保存按钮的状态:
SharedPreferences prefs = this.getSharedPreferences("MyApp", Context.MODE_PRIVATE);
boolean enabled = login.isEnabled();
prefs.edit().putBoolean("LOGIN_ENABLED_KEY", enabled).apply();
抓住login
中的onCreate
按钮后,检查是否存在此值(使用true
作为默认值):
SharedPreferences prefs = this.getSharedPreferences("MyApp", Context.MODE_PRIVATE);
boolean enabled = prefs.getBoolean("LOGIN_ENABLED_KEY", true);
login.setEnabled(enabled);
如果此时禁用该按钮,则需要重新启动计时器,以便最终启用它:
if (!enabled) {
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
login.setEnabled(true);
prefs.edit().clear().apply();
counter = 2;
}
}, 30000);
}
计时器结束时:
SharedPreferences prefs = this.getSharedPreferences("MyApp", Context.MODE_PRIVATE);
prefs.edit().putBoolean("LOGIN_ENABLED_KEY", true).apply();
或者,只需清除共享首选项:
SharedPreferences prefs = this.getSharedPreferences("MyApp", Context.MODE_PRIVATE);
prefs.edit().clear().apply();
把它们放在一起,大致是这样的:
int counter = 2;
Button login = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.loginview);
final SharedPreferences prefs = this.getSharedPreferences("MyApp", Context.MODE_PRIVATE);
login = (Button) findViewById(R.id.btnLogin);
boolean enabled = prefs.getBoolean("LOGIN_ENABLED_KEY", true);
login.setEnabled(enabled);
if (!enabled) {
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
login.setEnabled(true);
prefs.edit().clear().apply();
counter = 2;
}
}, 30000);
}
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
EditText a = (EditText) findViewById(R.id.etUser);
EditText b = (EditText) findViewById(R.id.etPassword);
String user = a.getText().toString();
String pass = b.getText().toString();
if (user.equals("") || pass.equals("")) {
Toast passed = Toast.makeText(LoginControl.this, "Please input required fields.", Toast.LENGTH_LONG);
passed.show();
} else if (pass.equals("pass")) {
Toast passed = Toast.makeText(LoginControl.this, "Success!", Toast.LENGTH_LONG);
passed.show();
// Start HomeControl + finish()
} else if (counter == 0) {
// Disable button after 3 failed attempts
login.setEnabled(false);
prefs.edit().putBoolean("LOGIN_ENABLED_KEY", false).apply();
Toast alert = Toast.makeText(LoginControl.this, "Login Disabled for 5 mins", Toast.LENGTH_LONG);
alert.show();
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
login.setEnabled(true);
prefs.edit().clear().apply();
counter = 2;
}
}, 30000);
} else {
Toast passed = Toast.makeText(LoginControl.this, "Username or password don't match!", Toast.LENGTH_LONG);
counter--;
passed.show();
}
} catch (Exception e) {
Toast passed = Toast.makeText(LoginControl.this, e.toString(), Toast.LENGTH_LONG);
passed.show();
}
}
});
}
(这里有一些重构的空间,但确实有效。)