i have got a login form, and i use sheredpreferences to store the login, if the user doesnt press the remember me button, i save the username during 5 minutes, after that time the username is deleted. if after the login ,and let´s say 3 minutes have passed, i close the app, now i dont need for the timer to execute, how can i close the timer from the activity that is running, in this case lertags.java?
heres the code...
login.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
}