如何停止计时器Android

如何停止计时器Android,android,service,timer,android-context,timertask,Android,Service,Timer,Android Context,Timertask,以下是我的代码: 在课堂上 private OnCheckedChangeListener alert_on_off_listener = new OnCheckedChangeListener(){ public void onCheckedChanged(RadioGroup groupname, int CheckedButtonId) { if(CheckedButtonId==R.id.radiobutton_on){ Toast.

以下是我的代码:

在课堂上

private OnCheckedChangeListener alert_on_off_listener = new OnCheckedChangeListener(){
    public void onCheckedChanged(RadioGroup groupname, int CheckedButtonId) {

         if(CheckedButtonId==R.id.radiobutton_on){
             Toast.makeText(getApplicationContext(), "radio on", Toast.LENGTH_LONG).show();
             alert_on = true;
             display_alert();    
         }
         else{
             alert_on = false;
         }   
    }
};

public void display_alert(){

    int delay = 10000;  
    Timer timer =new Timer();
    timer.scheduleAtFixedRate(new TimerTask()
    {
        public void run() 
            {
                Intent myIntent = new Intent(HoraWatchActivity.this,MyService.class);
                startService(myIntent);
            }
    }, delay,10000);
}
MyService.class

@Override
public void onStart(Intent intent, int startId) {
    super.onStart(intent, startId);
    if(MainActivity.alert_on)
       Toast.makeText(getApplicationContext(), "Alert is On", Toast.LENGTH_LONG).show();
    else
       Toast.makeText(getApplicationContext(), "Alert is Off",Toast.LENGTH_LONG).show();
}
我正在检查onCheckedChangeListener,并根据选中的单选按钮调用display_alert函数

在display_警报函数中,我使用固定速率10秒的计时器调度,并调用myservice类

因此,一旦我检查了单选按钮,它就会调用我的显示警报功能,并在toast中打开警报。所以它工作得很好

如果我再次选中radio_off按钮,然后再选中radio_on按钮,我会收到toast的警报,但toast会出现两次。同样,如果我再次选择单选按钮,土司将第三次显示,但我只需要一次。这是因为计时器每10秒运行一次。我想在单击单选按钮后停止计时器

问题是,一旦我启动了计时器,我就不再停止它了。我应该在什么时候以及如何取消课堂上的计时任务

if(timerTask!=null){
    timer.cancel();
}
您可以使用该代码停止/取消计时器


您可以使用该代码停止/取消计时器

首先创建一个扩展TimerTask的类,使其成为对象并处理该对象上的操作

int delay = 10000;  
Timer timer =new Timer();
myTimer mTask= new myTimer();
timer.scheduleAtFixedRate(mTask, delay, 10000);

public class myTimer extends TimerTask
{
    @Override
    public void run() {
        Intent myIntent = new Intent(HoraWatchActivity.this,MyService.class);
            startService(myIntent);         
    }
}
当你的mTask正常工作时,请立即恢复

 if(mTask!=null){
    mTask.cancel();
}

就是这样。

首先创建一个扩展TimerTask的类,将其作为对象并处理该对象上的操作

int delay = 10000;  
Timer timer =new Timer();
myTimer mTask= new myTimer();
timer.scheduleAtFixedRate(mTask, delay, 10000);

public class myTimer extends TimerTask
{
    @Override
    public void run() {
        Intent myIntent = new Intent(HoraWatchActivity.this,MyService.class);
            startService(myIntent);         
    }
}
当你的mTask正常工作时,请立即恢复

 if(mTask!=null){
    mTask.cancel();
}

就这样。

首先,以单独的方式创建TimerTask:

class MyTimerTask extends TimerTask {
  public void run() {
    Intent myIntent = new Intent(HoraWatchActivity.this,MyService.class);
    startService(myIntent);
  }
}
在某个地方声明它,当然对OnCheckedChangeListener可见:

MyTimerTask myTimerTask;
附表:

public void display_alert() {
  int delay = 10000;  
  myTimerTask = new MyTimerTask();
  Timer timer = new Timer();
  timer.scheduleAtFixedRate(myTimerTask, delay, 10000);
}
然后将代码修改为:

private OnCheckedChangeListener alert_on_off_listener = new OnCheckedChangeListener(){
public void onCheckedChanged(RadioGroup groupname, int CheckedButtonId) {

     if(CheckedButtonId==R.id.radiobutton_on){
         Toast.makeText(getApplicationContext(), "radio on", Toast.LENGTH_LONG).show();
         alert_on = true;
         display_alert();    
     }
     else{
         myTimerTask.cancel();
         alert_on = false;
     }   
}
};

首先,以单独的方式创建TimerTask:

class MyTimerTask extends TimerTask {
  public void run() {
    Intent myIntent = new Intent(HoraWatchActivity.this,MyService.class);
    startService(myIntent);
  }
}
在某个地方声明它,当然对OnCheckedChangeListener可见:

MyTimerTask myTimerTask;
附表:

public void display_alert() {
  int delay = 10000;  
  myTimerTask = new MyTimerTask();
  Timer timer = new Timer();
  timer.scheduleAtFixedRate(myTimerTask, delay, 10000);
}
然后将代码修改为:

private OnCheckedChangeListener alert_on_off_listener = new OnCheckedChangeListener(){
public void onCheckedChanged(RadioGroup groupname, int CheckedButtonId) {

     if(CheckedButtonId==R.id.radiobutton_on){
         Toast.makeText(getApplicationContext(), "radio on", Toast.LENGTH_LONG).show();
         alert_on = true;
         display_alert();    
     }
     else{
         myTimerTask.cancel();
         alert_on = false;
     }   
}
};

我能够使用以下代码停止计时器和任务:

    if(null != timer)
    {

        timer.cancel();
        Log.i(LOG_TAG,"Number of cancelled tasks purged: " + timer.purge());
        timer = null;
    }

    if(task != null)
    {
        Log.i(LOG_TAG,"Tracking cancellation status: " + task.cancel());
        task = null;
    }

我能够使用以下代码停止计时器和任务:

    if(null != timer)
    {

        timer.cancel();
        Log.i(LOG_TAG,"Number of cancelled tasks purged: " + timer.purge());
        timer = null;
    }

    if(task != null)
    {
        Log.i(LOG_TAG,"Tracking cancellation status: " + task.cancel());
        task = null;
    }