-1

我有一个登录表单,我使用 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

}
4

3 回答 3

0

将任务存储在类的成员变量中,并在您不再希望它发生时调用它的取消(例如在 onPause 或 onStop 中,具体取决于您想要做什么)。

于 2013-01-29T18:48:47.203 回答
0

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

updateProfile.cancel();

为此,您必须存储“updateProfile”变量的引用。更多详情请点击此处此处

于 2013-01-29T19:04:52.190 回答
0

在所有活动中维护全局变量的最佳方法是在Application类中维护自定义计时器变量

1)创建一个A扩展的类(示例)Application

TimerTask updateProfile2)在 Class 中创建实例A

3) 在类中创建方法来启动和取消定时器任务A[Eg: startTimer(), cancelTimer()]

3) 现在任何Activity用户都可以访问它。

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

于 2013-01-29T19:18:52.020 回答