How do I gracefully handle an Android service being killed by OS? -
i have service required run long periods of time in background. foreground service not suitable due notification. needs run quietly in background doing it's polling. , behaves should. not problem, background.
when service killed user , ondestroy() called, data saved db - no problem there. when phone turned off have broadcast receiever listens screenoff/on. again no problem saving there.
the problem when os kills service due low memory. service killed , don't believe ondestroy() called. if is, data not stored. if service killed , has been running while, information has gathered lost.
so after that, question is: how should gracefully clean resources , save data? there way can run code save data before system kills service? have considered making service save database @ intervals of few minutes reduce amount of data loss. not sure how performance wise. queries inserting 30 rows in period of 5 minutes. less.
i relatively new android development , have tried best problem before coming here ask question. apologies if have overlooked something.
thanks!
if service killed os, non of code executed on event, need save data once changed.
if fact, background service restart if return service.start_sticky_compatibility in onstartcommand(), it's default choice , don't need worry it. take quick initialization in service , save result possible.
Comments
Post a Comment