Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a timer is still running or not?

Tags:

android

timer

I try to send sms in service.If the sms is not sent means I restart the service after some time, for that I Using the timer.If the sms sent means I want to stop the timer,for stop the timer I use timer.cancel(); before that I've to check the timer is running or not.how to check it?.

             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;

                 }

Timer Schedule function

    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);
   }    
like image 295
Ram Avatar asked Aug 20 '13 07:08

Ram


1 Answers

There is no need to check if the timer is running if all you want to do is cancel() it (This is true for both the Timer and the TimerTask).

The documentation states that

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

like image 73
Jave Avatar answered Sep 30 '22 12:09

Jave