title: Android service restarting not working
body: <p>I have build a service to broadcast a timer data to various activities. Usually it runs ok without problems, but there are ocassions when android restarting it does not work as expected.</p>
  
  <p>It has also three Broadcast receivers:</p>
  
  <p>1 - Screen off event: in order to stop broadcasting timer data and setting an alarm in alarmanager for when the service ends, so I will be able to play end notifications for the users to attend.</p>
  
  <p>2 - Screen on event: in order to continue broadcasting the timer data. I also cancel any preiously alarm pending.</p>
  
  <p>3 - A receiver for alarms. This usually fires when screen is off, as explained in 1.</p>
  
  <p>My service is started with startForeground and return START_REDELIVER_INTENT. AS the start intent has inicial timer date, I can recreate service status without problem. Broadcast alarm receiver and onStartCommand share the same handle intent routine to start or continue the service.</p>
  
  <p>All this works perfect. For short timer ( &lt; 30 min ) I dont find any problem. The screen can be on, off, change same times from on to off and from off to on. Also the activity can be in front or in back. I have toying with those all possible states. In all cases, my service and activity runs ok.</p>
  
  <p>My problems comes when some timer are longer ( > 30 min , usually I set up for 35 min). There are sometimes that maybe due to memory reason Android kill me service. That is ok, as I understand Android does this in order to improve user experience. The problem is that when I goes to 'settings/application/services' I can see my service in 'restarting' state. I suspect this mean Android has not launched it yet and it is been sheduled. It show that state for long time (I have not had patient to say if it changes for more that other half hour....)</p>
  
  <p>The problem is that when in that state, that can long for ever (I have been looking into it and the service is never started), the timer (in my watch) reach the timeout, the alarmmanager launch my intent, but as my service is not started yet and the broadcast not registered, I cann't do staff for time outs (playing notifications). So the user does not known the service timer has ended and it is a heavy problem.</p>
  
  <p>It is very courious. As the service is killed in almost any run passed 30 min, while other services are not killed.</p>
  
  <p>My question is: what exactly is it happening? How can I correctly handle this situation in order to detect alarms been fired correctly, or more to the point: how can I force my service to be restarted correctly?</p>
  
  <p>For adding some help data:</p>
  
  <ul>
  <li><p>My service does have very little memory compare to others, and also does not run long operations, it only uses handler.postdelayed("sendUpdatesToUI",250) when screen is on, and nothing when screen is off, only waiting for alarmmanager to send time out intent.</p></li>
  <li><p>When time is reached, and user opens the activity, it received the timer data broadcast intent from service and as it see time is reached, then it stops the service.</p></li>
  <li><p>I understand and accept user can kill the service when he wants, and I accept that. The problem here is not the user, but Android restarting the service.</p></li>
  <li><p>When service killed, onDestroy is not invoked.</p></li>
  <li><p>Using 2.3.4 version.</p></li>
  </ul>