views:

184

answers:

2

I have a service that is defined as:

public class SleepAccelerometerService extends Service implements SensorEventListener

Essentially, I am making an app that monitors accelerometer activity for various reasons while the user sleeps with his or her phone/device on the bed. This is a long-running service that MUST NOT be killed during the night. Depending on how many background apps and periodic processes occur during the night, android sometimes kills off my process, thereby ending my service. Example:

10-04 03:27:41.673: INFO/ActivityManager(1269): Process com.androsz.electricsleep (pid 16223) has died.
10-04 03:27:41.681: INFO/WindowManager(1269): WIN DEATH: Window{45509f98 com.androsz.electricsleep/com.androsz.electricsleep.ui.SleepActivity paused=false}

I do not want to force the user to have 'SleepActivity' or some other activity in my app as the foreground. I can't have my service run periodically, because it is constantly intercepting onSensorChanged.

Any tips? source code is here: http://code.google.com/p/electricsleep/

+2  A: 

Keep your service footprint small, this reduces the probability of Android closing your application. You can't prevent it from being killed because if you could then people could easily create persistent spyware

BeRecursive
Or you could start another service and allow them to watch one another to ensure that it they do get killed then the other restarts it. Ugly but possibly effective
BeRecursive
@BeRecursive I do need to optimize the memory that my service consumes (I'm going to just dump the data to a SQLite db on file), especially because it inherently grows over time, however, I'm wondering why my service is being killed while other services continue to run. I know there is a priority system built into services- how might I increase my service's priority?
Jon
@Jon - If your service is killed it will eventually be restarted. I know you said you didn't want to but the only way to increase priority is by convincing the OS that the service is currently active to the user (in the foreground - using startForeground(int, Notification))
BeRecursive
It is *not* true that keeping it small reduces the probability of it being killed (temporarily until it restarts). The exact algorithm used here has varied across releases, but generally it *ensures* that background services will occasionally be killed/restarted, regardless of the memory they are using.
hackbod
+1  A: 

For Android 2.0 or later you can use the startForeground() method to start your Service in the foreground.

The documentation says the following:

A started service can use the startForeground(int, Notification) API to put the service in a foreground state, where the system considers it to be something the user is actively aware of and thus not a candidate for killing when low on memory. (It is still theoretically possible for the service to be killed under extreme memory pressure from the current foreground application, but in practice this should not be a concern.)

The is primarily intended for when killing the service would be disruptive to the user, e.g. killing a music player service would stop music playing.

You'll need to supply a Notification to the method which is displayed in the Notifications Bar in the Ongoing section.

Dave Webb
Thank you. I had heard of startForeground in passing before, but I didn't expect that startForeground would be a method in Service (though it makes sense that it would be there now that I think about it.)
Jon