Re: [android-developers] Long running Android service is getting recreated over and over
09.05.2012 23:17, William Kelley написал:
> So, if I want a long running process (logging GPS points over several
> hours), I would need to use START_STICKY and then maintain some
> knowledge of state between service restarts.
If you wanted it to be invisible to, I don't know, 80% of the users
(that number is a pure guess).
The supported way to do this is to use startForeground, which:
1) Tells Android that your service is doing something important
2) Tells the user about it via a required status bar icon
> That is, save state to disk (filename that I am writing to,
> specifically). And since onDestroy is not called and there is no other
> warning that the service is about to end, I can't use any kind of
> buffered file writer because I can't guarantee that the buffer is
> written and I can't know to call flush.
>
> Am I understanding this correctly?
With regards to buffering, I think yes.
You will see the onCreate, then onStartCommand, possibly with null intent.
Regarding this being an anti-pattern, I'm not sure I fully agree with
the widespread opinion on higher battery consumption.
But it's the user's perception that matters here, and it's very much
against long running services, to the point where I'm sometimes asked
why there are three (!!!!!) services in my app, and wouldn't it be
better to use one to reduce memory usage by a factor of three, obviously
(the answer is no... since the services are sharing the same process).
>
> And thanks Kostya for the info and very useful link.
>
> Thanks!
--
Kostya Vasilyev
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home