Android Stop Background Music

前端 未结 9 955
生来不讨喜
生来不讨喜 2020-12-01 02:29

EDIT - FOUND A EASY 5-10 LINE SOLUTION!!! See MY OWN ANSWER BELOW!!! YAY!!!!!!!!!

I\'ve searched for 5 hours, dozens of SO posts, no answers, and this seems like t

9条回答
  •  攒了一身酷
    2020-12-01 02:38

    In each activity, bind to the service in onStart() and unbind from the service in onStop(). When start the service, only bind to it and do not call startService(...).

    By calling startService() the service will continue to run until stopService() is called, regardless of the number of activities bound to it. If you don't call start service, then the service will automatically stop once all activities have unbound from it.

    So by binding and unbinding on each activity's onStart() and onStop() methods, you will find that the service (music) will continue until you leave your application with the home button, back key, screen timeout, etc.

提交回复
热议问题