-1

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

}
Hugo Silva
  • 23
  • 1
  • 10

3 Answers3

0

Store the task in a member variable of your class, and call cancel on it when you no longer want it to happen (say in onPause or onStop, depending on exactly what you want to do).

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
  • The timer is created in the login.java, and a i want to cancel in login.java, how can i pass the timer between these 2 activities? that is possible in the same activitie, am i correct? – Hugo Silva Jan 29 '13 at 19:06
0

Its very simple. As you are using your CustomTimerTask, simply when your application is going to close let say onDestroy () or any other exit point of your application,you write this line:

updateProfile.cancel();

For this you have to store reference of " updateProfile " variable. For more detail click here or Here.

Community
  • 1
  • 1
Mohammad Imran
  • 3,264
  • 4
  • 23
  • 37
0

Best thing to maintain the global variable across all activity is to maintain the custom timer variable in Application class

1) Create a class(Example A) which extend Application

2) Create TimerTask updateProfile instance in Class A.

3) create method to start and cancel the timer task in Class A [Eg: startTimer() , cancelTimer()]

3) Now this is accessible from any Activity user is navigating to.

4) from any activity you can cancel the timer [Eg: ((A)getApplicationContext()).cancelTimer()].

Darth Hunterix
  • 1,484
  • 5
  • 27
  • 31
Prasad
  • 240
  • 3
  • 7