user849998
user849998

Reputation:

How to force onDestroy method of the Service

Is there a way to force onDestroy method of the Service from the Android OS or, say, explicitly?

Ok, I think I found a way how to do it manually. I think that if you declare a service as a remote(Only for debugging purposes, because remote service is not a best app design case):

    <service android:name="widget.SomeWidget"
        android:process=":remote" />

And then in the DDMS Devices overview you can just stop the remote service. I believe it has the same affect as killing the process by the Android OS.

Result from LogCat:

INFO/ActivityManager(38): Process asd.asd.asd:remote (pid 4104) has died.
INFO/ActivityManager(38): Start proc asd.asd.asd:remote for service asd.asd.asd/widget.SomeWidget: pid=4230 uid=10028 gids={3003, 1015}

Upvotes: 0

Views: 1938

Answers (3)

Archit
Archit

Reputation: 887

The OS killing the service is not controlled by you. The best you can do is call Context.stopService()
The closest workaround would be to try something like this:

Runtime.getRuntime().exec("kill "+processId);

Where processId is the PID of the process you want to kill. You will need a rooted phone for this.

Upvotes: 1

Reno
Reno

Reputation: 33792

No it makes more sense to call yourOwnDestroy() method which handles the clean up tasks, through the service binder interface; relying on onDestroy() to be called is not recommended.

onDestroy() will be scheduled to be called some time after unbindService() and stopService()

Upvotes: 1

brianestey
brianestey

Reputation: 8302

Call Context.stopService() to stop the service, or stopSelf() from inside the service.

Upvotes: 0

Related Questions