我有一个登录表单,我使用 sheredpreferences 来存储登录信息,如果用户没有按记住我按钮,我会在 5 分钟内保存用户名,然后删除用户名。如果登录后,假设 3 分钟过去了,我关闭了应用程序,现在我不需要执行计时器,我如何从正在运行的活动中关闭计时器,在这种情况下是 lertags.java?
这是代码...
登录.java
CheckBox checkBox = (CheckBox) findViewById(R.id.silent_switch);
if(checkBox.isChecked())
{
//save data username and password
SharedPreferences.Editor prefEditor = gameSettings.edit();
prefEditor.putString("UserName",txtperson.getText().toString());
prefEditor.putString("Password", txtpass.getText().toString());
prefEditor.commit();
}
else
{
//create timer for 10 seconds,after that delete the user
TimerTask updateProfile = new CustomTimerTask(Login.this);
timer.schedule(updateProfile, 10000);
}
//save the value user and the poosition
SharedPreferences.Editor prefEditor = gameSettings.edit();
prefEditor.putString("User",user);
prefEditor.putString("Posto", posto);
prefEditor.commit();
//mensagemexibir("Login", "Welcome: "+user);
Intent i = new Intent(Login.this, LerTags.class);
startActivityForResult(i, 2);
setResult(2);
finish();
CustomTimertask.java
public class CustomTimerTask extends TimerTask {
private Context context;
private Handler mHandler = new Handler();
// Write Custom Constructor to pass Context
public CustomTimerTask(Context con) {
this.context = con;
}
@Override
public void run() {
// TODO Auto-generated method stub
// your code starts here.
// I have used Thread and Handler as we can not show Toast without starting new thread when we are inside a thread.
// As TimePicker has run() thread running., So We must show Toast through Handler.post in a new Thread. Thats how it works in Android..
new Thread(new Runnable() {
public void run() {
mHandler.post(new Runnable() {
public void run() {
Toast.makeText( context, "10 seconds pass delete the user", Toast.LENGTH_LONG).show();
SharedPreferences.Editor prefEditor = gameSettings.edit();
prefEditor.putString("User","");
prefEditor.commit();
}
});
}
}).start();
}
public void startact() {
// TODO Auto-generated method stub
}