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

Posted

技术标签:

【中文标题】从另一个活动中删除计时器【英文标题】:Delete timer from another activity 【发布时间】:2013-01-29 18:45:29 【问题描述】:

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


【问题讨论】:

你有解决方案吗?查看我的答案 【参考方案1】:

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

【讨论】:

计时器是在 login.java 中创建的,我想在 login.java 中取消,我如何在这两个活动之间传递计时器?这在同一个活动中是可能的,我说的对吗?【参考方案2】:

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

updateProfile.cancel();

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

【讨论】:

【参考方案3】:

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

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

2) 在A 类中创建TimerTask updateProfile 实例。

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

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

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

【讨论】:

以上是关于从另一个活动中删除计时器的主要内容,如果未能解决你的问题,请参考以下文章

如何在活动中显示计时器?

QObject::~QObject: 定时器不能从另一个线程停止

当活动不在前面时,在Android中暂停CountDownTimer

如何在使用片段和计时器的选项卡式活动上更新 UI

Android:当活动死亡时,计时器被孤立

改变QLabel的大小时,PyQt5“定时器无法从另一个线程启动”错误