从另一个活动中删除计时器

时间:2013-01-29 18:45:30

标签: android timer timertask

我有一个登录表单,我使用sheredpreferences来存储登录,如果用户没有按下记住我按钮,我会在5分钟内保存用户名,之后用户名将被删除。 如果在登录后,让我们说3分钟过去了,我关闭应用程序,现在我不需要执行计时器,如何从正在运行的活动关闭计时器,在这种情况下lertags.java?

继承人代码......

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

}

3 个答案:

答案 0 :(得分:0)

将任务存储在班级的成员变量中,并在不再需要时调用取消(例如onPause或onStop,具体取决于你想要做什么)。

答案 1 :(得分:0)

非常简单。当您使用CustomTimerTask时,只需在您的应用程序即将关闭时,让我们说onDestroy()或应用程序的任何其他出口点,您可以写下这一行:

updateProfile.cancel();

为此,您必须存储“updateProfile”变量的引用。有关详细信息,请点击hereHere

答案 2 :(得分:0)

在所有活动中维护全局变量的最佳方法是在Application

中维护自定义计时器变量

1)创建一个扩展A

的类(示例Application

2)在班级TimerTask updateProfile中创建A个实例。

3)创建方法以启动和取消课程A中的计时器任务[例如:startTimer()cancelTimer()]

3)现在可以从导航到的任何Activity用户访问。

4)你可以从任何活动取消计时器[例如:((A)getApplicationContext()).cancelTimer()]。

相关问题