Why Can't I Stop A Service In Android?
Intent helloservice = new Intent(this, HelloService.class); startService(helloservice); ... stopService(helloservice); Why won't this work? It just keeps running. Am I missing s
Solution 1:
To make the Timer
go away when your service is stopped, you need to call cancel()
on the Timer
in the appropriate callback method for the service; onDestroy
by the looks of it.
Even if I stop my service...my timer runs? Why?
Because, as far as the Android operating system is concerned, the Timer instance is not logically tied to any particular service. It is the Service implementation's responsibility to deal with releasing any resources that the garbage collector won't deal with. (Open file handles and database connections are other examples I imagine.)
Solution 2:
It does stop your service. Override onDestroy in your service and you'll see that it's called. But service != timer, you have to stop your timer manually.
Post a Comment for "Why Can't I Stop A Service In Android?"