开发者

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开发者_如何学JAVA 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}


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.


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()


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜