Why do we need a service on Android? Music playback can be done with background stream

I was asked this question in an interview. I'm not sure if this is the right forum to ask this question because it doesn't contain any code and understands Android concept.

Question: "Why do we need a service when everything can be done by a background thread in Android?" The service runs on the main thread, why do we need something running on the main thread, but in the background? Examples like music playback can be done on a background thread too, so why do we need a service.

Please let me know if this needs to be asked on another forum.

+3


source to share


1 answer


Services: A service is similar to an Activity. but it doesn't require an interface. whereas in a thread created from an activity, the thread will run until the activity is running. so if you stream music it will work. but it will fire when the action ends, whereas when you have played music from the service, it will run the entire life cycle of the service.

See my boundservice answer for communication between service and activity. when your music player is running you must keep the notification on the notification bar.



UPDATE

When you play music with background thread, the music will play, even the app closes the background thread, becomes the stream of the orphanage. You cannot control the status of the music player. If you work with a service, the music will play and it will not become an orphanage when you exit the application. When you step back from the app, you can chat with the music player, whereas the background thread cannot.

+4


source







All Articles