title: android how to deal with android timer when phone goes to sleep
body: <p>Our Application has a service which starts a timer to schedule a task every 4 hours.<br>
  But the phone often goes to sleep when the screen is off.<br>
  I don't expect the task to run when the phone is sleeping.<br>
  But I want that it can be run immediately if the phone has slept longer than 4 hours.</p>
  
  <pre><code>TimerTask task = new TimerTask() 
  {
  
              @Override
      public void run() {
                            //do something
          }
      };
  
  new Timer().schedule(task, 0,  4*60*60*1000);//period=4 hours;
  </code></pre>
  
  <p>I have done some tests that show if then phone has slept for more than 4 hours and when it wakes up, the timer's period didn't add the past 5 sleep hours and it can't run the task immediately.<br>
  Its period time just calculates the phone's wake time.<br>
  I have studied Timer.java, and know the timer's delay mechanism is to use thread wait(), that is the reason.<br>
  How to make a cyclic time task that contains the phone sleep time?<br>
  I think there is no need to use an AlarmManager, because the task doesn't need to run when the phone is sleeping.<br>
  Thanks in advance.</p>