Android Stop Service on Crash

让人想犯罪 __ 提交于 2019-12-10 21:36:04

问题


I am running a Service and am wondering in the rare case that my app crashes will it automatically kill my Service too? I don't want it continuing if this happens.

If not, is there a way to do this such as in the onDestroy() method?


回答1:


I've done a bit of research, and I think I have a complete answer to your question.

Subclassing Service is fine, assuming you're not using a Remote Service (a Service in a different process.) Assuming your Service is in the same process as your Activity, they'll both end together, in the event of a crash. That being the case, my earlier suggestion to use BoundService wouldn't have actually produced a different result and was based on a flawed understanding of how Application Components work in Android processes.

Quoting from Android bound service - should I manually reconnect in onServiceDisconnected or it tries reconnect automatically:

Local Service:

Service is running in the same process as other components (i.e. activity that bound to it) from the same application, when this single application-scoped process has crashed or been killed, it is very likely that all components in this process (include the activity that bound to this service) are also destroyed.

Also, you can read Remote Service Vs. Local Service.



来源:https://stackoverflow.com/questions/19057970/android-stop-service-on-crash

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!