Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialRitesh goel
2,967 PointsWhy are songs getting downloaded sequentially?
Since we are creating a separate service for each song and each service has its own thread then, why is the download of next song starting after ending of previous song?
2 Answers
Ben Deitch
Treehouse TeacherJust because we're starting the Service for each song doesn't mean we're creating a new Service for each song. I'd bet it's all the same Service object; an easy way to find out would be to put a log statement in the Service's onCreate function and see what it logs.
Michael Nock
Android Development Techdegree Graduate 16,018 PointsFrom the Android documentation:
There are two reasons that a service can be run by the system. If someone calls Context.startService() then the system will retrieve the service (creating it and calling its onCreate() method if needed) and then call its onStartCommand(Intent, int, int) method with the arguments supplied by the client. The service will at this point continue running until Context.stopService() or stopSelf() is called. Note that multiple calls to Context.startService() do not nest (though they do result in multiple corresponding calls to onStartCommand()), so no matter how many times it is started a service will be stopped once Context.stopService() or stopSelf() is called; however, services can use their stopSelf(int) method to ensure the service is not stopped until started intents have been processed.
As Ben mentions, it appears the service only gets created once, after that it just retrieves the first service object we created and uses that.