Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to bind a service and when not to bind a service

I have been paging through the Android documentation and I was curious. When would you bind a service as opposed to not binding the service? What advantages/limitations does it provide?

like image 313
Andrew Guenther Avatar asked Dec 05 '25 23:12

Andrew Guenther


1 Answers

When would you bind a service as opposed to not binding the service?

A full answer to that requires a few pages in (::ahem::) a book. :-)

Binding to a service presents challenges when it comes to deal with configuration changes, such as screen rotations. Hence, all else being equal, using the command pattern (startService()) beats using the binding pattern (bindService()).

You have to use the command pattern if you want your service to run without any activity around to be bound to it. So, a music player, a large-file downloader, or a cron job set up with AlarmManager would all tend to use the command pattern.

Binding gives you access to a richer API, including support for data types that will not work with the command pattern (which is limited to things you can stick in a Bundle).

like image 84
CommonsWare Avatar answered Dec 08 '25 14:12

CommonsWare