views:

332

answers:

2

Hi,

We have a Service that continuously collects sensor data on the phone. This service should run "forever", e.g. as long as the user wants, and not be killed by the system.

For clarification, this service is not intended for an app to be released in the market to the general public, it is written for a scientific study. So the people running the app are fully aware that their battery will sucked empty faster than usual, this is no problem.

Anyways, my problem is that the service gets killed after a while of running. Sometimes after an hour, sometimes only after like 7 oder 10 hours.

The Log-Entries when the service gets killed look like this. It just says "no longer want", sometimes without even calling onDestroy(), as far as I can tell.

07-20 17:07:11.593 I/ActivityManager(   85): No longer want my.project.datalogging (pid 23918): hidden #16
07-20 17:07:11.593 I/WindowManager(   85): WIN DEATH: Window{44c61570 my.project.datalogging/my.project.datalogging.DataLoggingApp paused=false}
07-20 17:07:11.603 I/BackgroundService(23925): onDestroy()

Or later (after I manually restarted it):

07-20 19:00:49.677 I/ActivityManager(   85): No longer want my.project.datalogging:BackgroundService (pid 24421): hidden #17
07-20 19:00:49.677 I/ActivityManager(   85): No longer want my.project.datalogging (pid 24415): hidden #18
07-20 19:00:49.807    85 10707 I WindowManager: WIN DEATH: Window{44f1ea58 my.project.datalogging/my.project.datalogging.DataLoggingApp paused=false}

I often see other services being killed with "no longer want" and then immediately restarted with "Scheduling restart of crashed service". For example here with runkeeper:

07-20 17:30:45.503 I/ActivityManager(   85): No longer want com.fitnesskeeper.runkeeper (pid 24090): hidden #16
07-20 17:30:45.603 W/ActivityManager(   85): Scheduling restart of crashed service com.fitnesskeeper.runkeeper/.services.RunKeeperService in 5000ms
07-20 17:33:52.989 I/ActivityManager(   85): Start proc com.fitnesskeeper.runkeeper for service com.fitnesskeeper.runkeeper/.services.RunKeeperService: pid=24292 uid=10099 gids={3003, 1015}

The logs show no record of low memory. Testing is done on (several) Nexus One with 2.2 (Froyo FRF91).

Is there any way I can achieve this behaviour with my app? Automatic restarting after being killed?

Or this this something totally different that just looks similar in logcat?

If you need any more information, just ask and I'll try to provide it :-)

+2  A: 

How do you start the service? Have you tried startForeground()? That's not an absolute guarantee, of course, but should at least lengthen the lifespan.

Pontus Gagge
Thanks for your reply. We don't use startForeground(), but it seems like a good idea. I implemented it and I'm currently testing to see if it hels. This can take a while, since the service was already able to survive several hours without getting killed.
pableu
I'm not sure if there's a standard Android way of guaranteeing the service stays up, while working in the Java space (a separate watchdog service, scheduled to run periodically and check on your service still being up?). If not, you may need to go down into the NDK and Linux layer...
Pontus Gagge
Thanks for the startForeground() idea, the service has now been running for almost 24 hours on two separate phones and wasn't killed. Seems to do the trick :-)
pableu
A: 

Try overriding the onCreate() method of your service. If the service is killed and then restarted it's onCreate() is called, but not onStart(). So you can, for example, add call to onStart() from onCreate to make it behave like the RunKeeperService.

ptu