Advantages and disadvantages of binding versus running a service in android

Yes, I know this is a duplicate: Android service: bind or run? . But, as I commented there, this answer is problematic.

At Google Dev Tutorial, binding is recommended as ...would work well for a music application that needs to bind an activity to its own service that playing music in the background. . While the SO answer I just linked to says that I really need to start, not link.

Now I understand the argument for the Navigation application, but of course the evaluation is not suitable for things like streaming music. So someone will ask to clarify a little more?

The fact that I can endlessly connect and start endlessly suggests that two methods exist for some reason. More specifically, for example, why is streaming music a good example for ?? //

And again, my main question: what are the advantages and disadvantages with regard to binding to the start? In other words: compare and match bindService and startService.

Sorry for the repetitions, but I am trying to fix this as fully as possible in order to get really good answers.

+4
source share
1 answer

As for how I worked and read material about bindService and startService I explain the following.

First of all, they have a different life cycle.

 bindService(mIntent, mConnection, BIND_AUTO_CREATE); 

If you just use the bindService with the BIND_AUTO_CREATE flag to create the service and the binding, it will create the service for you and associate it with you. As soon as you unbindService service, the service life will be stopped. i.e. Most likely in the onStop phase of your main process. Thus, you can use bindService to create and bind a service if you need it only on demand and closes when you do not need it. As a navigation application, as you mentioned. This will not follow the usual Service life onCreate > onStartCommand

 startService(mIntent); bindService(mIntent, mConnection, 0); 

If you want to start the service, even if you quit Activity, you need to use startService and you can bind it to it using a bindService with a flag, for example 0 or BIND_ABOVE_CLIENT (which means the service is more important than running client activity). Now you can bind to the service and unbind any number of times, but the service will continue to work (until you kill it). It follows onCreate > onStartCommand

Your confusion with the music application is mainly related to the use of this application: in Google Dev, they might notice an aspect such as when you close your activity, the music stops and leaves the application (for example, Youtube). [enough snapping and unlinking]

And SO could point out that music should continue even after leaving the game (e.g. Google Play Music). [for this you must start the service and start it forever]. You can stop the service by calling this.stopSelf() when you need, or stopService from the action.

Thus, you cannot say that bindService better citizen than a running service.

A service created using bindService will not work after an action is stopped.

+2
source

All Articles