android – 如何检查计时器是否仍在运行?

前端之家收集整理的这篇文章主要介绍了android – 如何检查计时器是否仍在运行?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我尝试在服务中发送短信.如果没有发送短信意味着我在一段时间后重新启动服务,为此我使用定时器.如果发送的短信意味着我想停止定时器,为了停止定时器我使用定时器.取消();在此之前,我要检查计时器是否正在运行或不检查它.
int resultCode = getResultCode();
                 switch (resultCode) 
                 {
                case Activity.RESULT_OK:                 
                             timer.cancel();//Here I want to check timer running or not
                 break;

                 case SmsManager.RESULT_ERROR_GENERIC_FAILURE:  
                     timer_run();
                 break;

                 case SmsManager.RESULT_ERROR_NO_SERVICE:     
                     timer_run();
                 break;

                 case SmsManager.RESULT_ERROR_NULL_PDU:        
                      timer_run();
                  break;

                 case SmsManager.RESULT_ERROR_RAdio_OFF:        
                     timer_run();
                  break;

                 }

定时器计划功能

public void timer_run(){        

    TimerTask hourlyTask = new TimerTask () {
        @Override
        public void run () {
             Intent myIntent = new Intent(Myservice.this,Myservice.class); 
             startService(myIntent);


        }
    };
     timer.schedule (hourlyTask,0l,500*5*5);
   }

解决方法

如果您要做的只是 cancel(),则无需检查计时器是否正在运行(对于 TimerTimerTask都是如此).

文件说明了这一点

This method may be called repeatedly; the second and subsequent calls have no effect.

原文链接:https://www.f2er.com/android/309166.html

猜你在找的Android相关文章