Android: stopService doesn't call onDestroy

前端 未结 4 1834
北恋
北恋 2020-12-11 01:13

I have big problems with stopping a service, which plays audio file. I want to stop the current service before I play another file.

Activity:

public vo         


        
4条回答
  •  死守一世寂寞
    2020-12-11 01:54

    I had a similar problem even with onDestroy correctly set up - the answer for which could be useful for people coming to this page. For me, onDestroy was not called immediately during stopService, sometimes for a very long time - leaving the service doing work when it should have stopped.

    The answer is that this is expected behaviour - I can guess but don't exactly know why. So my solution in this case was to create a public static method to stop the actions of the service manually, which happens at the time it's called, then stopService() will call onDestroy in it's own good time...

    In the case for this example (which is probably long gone, I would probably decide to keep the service running until it's expected to quite the app, but provide ways to change audio file, while the service is still active.

提交回复
热议问题