简体   繁体   中英

Android Service's onBind isn't being called and onDestroy is being called even with a client connected

In my app, I have a Service that is used to play music of a MP3 stream. I start it (startService()) and connect to it (bindService()), so it doesn't stop when there's no client connected. If the player is paused, I clean some resources after some time and, if it remains paused for some more time, I clean almost all the resources and: if there's no client connected (Activitys connected), I stop the service calling stopSelf(); if there's some client connected, I prepare it to be destroyed, setting a boolean that is checked when all the clients disconnect, calling stopSelf() if there's no client connected and this boolean is true.

So, basically, the service is destroyed only if (1) there's no client connected to it AND (2) it's paused for a long time.

It's working well, but I have an issue. In onDestroy(), I release all the resources and save some data (like the list of musics that was being played). If there's many musics, it can take almost half a second. The problem is when I open again my app exactly in this period of time, when the onDestroy() hasn't returned yet. What happens is that the same service's instance is used and onCreate() isn't called. So, since the resources were released, naturally I get some errors.

This bug is very hard to simulate. I was able to reproduce just 3 times. I don't know how to proceed. I thought of returning from the onDestroy() as fast as I can, using another thread to release the resources and save the data, but it would make the new service to be started before the data has been saved. Does someone know more details about the way the destroy/recreate of a service is done on Android? I don't know which class manages it, so I don't know which code I could read to understand it better.

Thanks in advance.

UPDATE:

I've just found my real problem. It seems that the Service's onBind isn't being called when my Activity disconnects and reconnects (when I close all the Activities and open the app again). So, my resource cleaner (that is a scheduled job that is scheduled for some time later when the player is paused) thinks there's no client connected and calls stopSelf() (I keep track of the connected clients using the onBind and onUnbind methods). And, even that there's a client connected to the service, the onDestroy() is called. Then, my Activity keeps connected to the Service that just cleaned all the resources.

I've updated the question's title as well.

UPDATE 2:

Just a little more explanation. When I close and reopen my app, the method onServiceConnected of my ServiceConnection's implementation is called after the bindService, but the onBind of the Service isn't called. And it's seems that it really thinks there's no client connected, since it gets destroyed even that the documentation says that a started and bound service wouldn't be destroyed until all the clients unbind.

I've found out that my onBind wasn't being called when reconnected with the Service. So, I started to return 'true' in the onUnbind. This way, the onRebind is called when the Activity reconnects to the Service. Then, I'm still keeping track if there's any client connected and my resource cleaner just calls stopSelf if there's no client connected.

Anyway, I think it is a bug. The documentation tells me that a Service that is both started (startService()) and bound (bindService()) isn't destroyed (onDestroy()) until both all clients are disconnected and the stopService/stopSelf is called. So, when my resource cleaner calls stopSelf(), the service shouldn't be destroyed, since there's a client connected (when the client reconnects).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM