Not getting onDestroy() is normal, expected, and desired behavior as for all app components -- the kernel kills processes when it needs more memory, we don't rely on them being nice and cleaning up at their leisure.
The current semantics for recovering from being killed are unfortunately as you describe. I actually just checked in a change for Eclair that improves this a lot, allowing you to much better control what happens after being killed, and detecting that you were killed when restarted. Until then, you'll need to take care to implement onCreate() to recover. As far as being foreground -- if you do indeed show a notification while running, then making yourself foreground is reasonable (since the user knows you are running, and can go through the notification to stop you), and will basically ensure that you aren't killed. Note though that another change in Eclair will be that setForeground() no longer does anything, and will be replaced with a new API that requires having a notification shown while being in the foreground state. (This is because numerous apps are now abusing setForeground() by just using it to avoid having their memory used when the system needs it, instead of to let the system know the user is actively aware of what they are doing and so shouldn't be killed.) It does sound a little odd to me that you say the service isn't important to the user, but you are posting a notification while it is running. If it isn't important to them, I would suggest it shouldn't be notifying them. ;) Another thing to think about -- if you are polling, ideally you wouldn't keep your service always running. Typically how people implement polling is by scheduling an alarm to start their service after the poll interval. Then if you get killed, you can always recover when the next alarm goes off. (You can either schedule a repeating alarm, or re-schedule your alarm in onStart(); you are guaranteed to not be killed except under severe circumstances while in onCreate() or onStart().) On Tue, Aug 25, 2009 at 5:54 AM, Chister Nordvik <[email protected]> wrote: > > Hi! > > I have a service that polls for data. To indicate this to the user I > have a persistent notification in the statusbar. In some cases when > the device goes low on memory it destroys the service but OnDestroy is > not called. Later when there is available memory OnCreate is called. > Is this normal behavior? I had hoped that OnDestroy would be called to > I could remove the notification in the statusbar. Now the user thinks > that the service is still running, while it has been stopped by the > OS. > > In order to restart the polling how do I know that the OnCreate is > really a restart event and not first time creation of the service? I > thought about checking for the presence of the notification in the > statusbar, but I couldn't find a API to check if a notification was > showing or not. > > I have not tried "SetForeground" on the service, since the service > isn't that important to the user, but maybe that would minimize the > problem... > > -Christer > > > -- Dianne Hackborn Android framework engineer [email protected] Note: please don't send private questions to me, as I don't have time to provide private support, and so won't reply to such e-mails. All such questions should be posted on public forums, where I and others can see and answer them. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/android-developers?hl=en -~----------~----~----~----~------~----~------~--~---

