Also, if you are writing an app where you have a service that is just being used by the code in that app (not being accessed by other apps), and not looking for the startService semantics... you should probably stop and make sure you really need a service at all. If you are just binding within your app, this doesn't give you anything -- the process is already running, the process importance is not going to change due to this. All you may need is just a singleton, which can be a lot simpler to implement.
On Mon, Jun 4, 2012 at 9:55 PM, Dianne Hackborn <hack...@android.com> wrote: > 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. > > 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. > > > On Mon, Jun 4, 2012 at 8:35 AM, Mark Murphy <mmur...@commonsware.com>wrote: > >> On Mon, Jun 4, 2012 at 11:30 AM, Greenhand <cooperateonl...@gmail.com> >> wrote: >> > If I use startService(), can the Activity communicate with the Service >> > and vice versa? >> >> startService() uses a variation of the command pattern: you >> "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. >> >> -- >> Mark Murphy (a Commons Guy) >> http://commonsware.com | http://github.com/commonsguy >> 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+unsubscr...@googlegroups.com >> For more options, visit this group at >> http://groups.google.com/group/android-developers?hl=en >> > > > > -- > Dianne Hackborn > Android framework engineer > hack...@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. > > -- Dianne Hackborn Android framework engineer hack...@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+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en