Service not always being restarted after being stopped
-
I'm developing an app for my tablet that is to turn on the screen when I rotate it to the vertical (the power switch is dodgy). It has an optimization to turn off the sensor from 22:00 to 06:30. The app works most of the time. However sometimes it fails. For example this morning I picked it up at 07:10 and the screen came on as expected. I put it down for about a half an hour and when I picked it up again it did not come on. I plugged in the power and it came on. I stopped my service and restarted it then and it has been running fine: pick up and it comes on when the screen has gone off. Looking at the logs I see that the system stops the service frequently(maybe every 30 minutes) and then restarts it, passing a null intent to the onStartCommand() method. I've used SharedPreferences to save the app's state and am able to successfully restart the app where it needs to be. My question: why didn't the app work this morning after it had worked on the first pickup of the morning? Any ideas on how to debug this or any work-arounds? It's a very elusive bug. The code works so many times and fails so few times that I'm not able to come up with a debugging scheme to find out a reason or pattern for the failure. Do you have any suggestions? Something I've observed by looking at the logs is that the system restarts my service (passing a null intent to the onStartCommand() method) very soon after I have plugged in the tablet to turn on the screen. How would a bug in my code cause that? Another observation: the system often restarts my service 5 seconds after the AlarmReceiver is supposed to receive an alarm: at 22:00:05 for an alarm set for 22:00:00. Also posted at: http://www.codeproject.com/Questions/826395/Service-not-always-restarted-on-ASUS-tablet?cmt=683080#cmt2\_826395
-
I'm developing an app for my tablet that is to turn on the screen when I rotate it to the vertical (the power switch is dodgy). It has an optimization to turn off the sensor from 22:00 to 06:30. The app works most of the time. However sometimes it fails. For example this morning I picked it up at 07:10 and the screen came on as expected. I put it down for about a half an hour and when I picked it up again it did not come on. I plugged in the power and it came on. I stopped my service and restarted it then and it has been running fine: pick up and it comes on when the screen has gone off. Looking at the logs I see that the system stops the service frequently(maybe every 30 minutes) and then restarts it, passing a null intent to the onStartCommand() method. I've used SharedPreferences to save the app's state and am able to successfully restart the app where it needs to be. My question: why didn't the app work this morning after it had worked on the first pickup of the morning? Any ideas on how to debug this or any work-arounds? It's a very elusive bug. The code works so many times and fails so few times that I'm not able to come up with a debugging scheme to find out a reason or pattern for the failure. Do you have any suggestions? Something I've observed by looking at the logs is that the system restarts my service (passing a null intent to the onStartCommand() method) very soon after I have plugged in the tablet to turn on the screen. How would a bug in my code cause that? Another observation: the system often restarts my service 5 seconds after the AlarmReceiver is supposed to receive an alarm: at 22:00:05 for an alarm set for 22:00:00. Also posted at: http://www.codeproject.com/Questions/826395/Service-not-always-restarted-on-ASUS-tablet?cmt=683080#cmt2\_826395
Quote:
the system stops the service frequently(maybe every 30 minutes) and then restarts it, passing a null intent to the onStartCommand() method
Instead of returning
START_STICKY
fromonStartCommand
try returningSTART_REDELIVER_INTENT
. This will ensure that the system redelivers the initial Intent that was passed to the service when the service was started. -
Quote:
the system stops the service frequently(maybe every 30 minutes) and then restarts it, passing a null intent to the onStartCommand() method
Instead of returning
START_STICKY
fromonStartCommand
try returningSTART_REDELIVER_INTENT
. This will ensure that the system redelivers the initial Intent that was passed to the service when the service was started.Thanks for the response. The null intent is not the problem. The code saves the data from the original intent and uses the startId value to recognize that is has been restarted. The problem is that my service is NOT being restarted promptly or some times not at all.
-
Thanks for the response. The null intent is not the problem. The code saves the data from the original intent and uses the startId value to recognize that is has been restarted. The problem is that my service is NOT being restarted promptly or some times not at all.
As you have mentioned that it is being killed frequently, it seems that the device is running low on resources and in such a case the system will not restart the Service until adequate resources are available. Try running the service in Foreground and see if it helps. However, if your device is running Android 4.4.2 then it's a known issue - https://code.google.com/p/android/issues/detail?id=63793[^]
-
As you have mentioned that it is being killed frequently, it seems that the device is running low on resources and in such a case the system will not restart the Service until adequate resources are available. Try running the service in Foreground and see if it helps. However, if your device is running Android 4.4.2 then it's a known issue - https://code.google.com/p/android/issues/detail?id=63793[^]
Thanks again. The link has some good ideas. My tablet is running version 4.3 I do use startForeground() method and it shows a Notification. The frequency of being stopped was almost exactly every 30 minutes. The times of some of the restarts are 5 seconds after an Alarm is supposed to go off and send an Intent to my service.