Googling around, I have seen this problem posted many times, some of the posts on this forum over that last few years. I have never seen anyone get a straight answer.
I have a foreground service that tries to stop itself after running for 2 hours by doing a this.StopSelf(). I've tried it when it's bound and when it's unbound. On the AVD and on a device.
It simply does not work. Is this a bug in Android? (running 2.3.3).
How can a service stop itself?
I encounter this issue of
selfStop()
that not working too.The main idea to understand is that the Service WILL NOT stop,
if a running loop is still running (
while(mDoWhile)
for example).Or any other issue that need to be destroyed / unregistered first.
So to make the
stopSelf()
orstopService(intent)
works,implement
onDestroy()
within your service:stopSelf
might not destroy the service if the conditions are not favorable, but you canstopService
with the application context. But make sure the service doesn't restart before callingstopService
inside service.