Re: [android-developers] Re: android bindservice rotation
Do please be careful about startService. You should only use one or the other where you want the associated semantics:
- startService means "I want you do go off and do this work independently of the caller, with the understanding that someone will take care of stopping the service as soon as it is done with that work."
- bindService means "I want to be a client of the service, to make use of this service and (optionally) keep it running while I need it."
A service can implement either protocol as needed, and there is no problem mixing them -- the service will just remain created as long as required by either of them.
A service can implement either protocol as needed, and there is no problem mixing them -- the service will just remain created as long as required by either of them.
However, these semantically are different. For example when you use bindService(), you are creating a connection between the caller and the service, with the caller being a client. This means for example that process management of the service is dependent on the client -- if the client is in the foreground, then the service process can also be brought to the foreground; while if the client is in the background, it won't make the service process any more important than other background processes. Contrast this with startService(), where for as long as the service is in the started state it will be managed as at least a service-level process as per http://developer.android.com/guide/topics/fundamentals/processes-and-threads.html#Lifecycle and the code that called startService() won't cause it to go higher in priority even if the caller is in the foreground.
--
Dianne Hackborn
Android framework engineer
hackbod@android.com
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.
-- On Mon, Jun 4, 2012 at 8:35 AM, Mark Murphy <mmurphy@commonsware.com> wrote:
On Mon, Jun 4, 2012 at 11:30 AM, Greenhand <cooperateonline@gmail.com> wrote:startService() uses a variation of the command pattern: you
> If I use startService(), can the Activity communicate with the Service
> and vice versa?
"communicate" with the service via extras on the Intent passed to
startService() as the command. The service can then use a Messenger,
or broadcasts, or local broadcasts, or a PendingIntent, etc. to
trigger UI updates based upon work that was completed.
You are welcome to use both binding and startService(), if you wish,
in which case the latter is simply to give the user more direct
control over the start/stop of the service itself, with the binding
for other communication.
--
http://commonsware.com/blog | http://twitter.com/commonsguy
Android Training in DC: http://marakana.com/training/android/
--
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
Dianne Hackborn
Android framework engineer
hackbod@android.com
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 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