If Android service crashes will it bring down App

2019-05-07 20:14发布

I'm writing a service for an Android application component and I want my App to not crash because of any fault in service. By default Is the app is protected from any faulty behavior of background service.

2条回答
看我几分像从前
2楼-- · 2019-05-07 20:43

One option is to run your Service in a separate process from the rest of your app. If your Service crashes, this will not directly crash your app (ie: your activities). However, you then need to figure out how your app should continue to operate once the Service has crashed. Of course, your app can attempt to restart the Service and/or take other measures.

To do this, just add this to the <service> declaration in your manifest:

android:process=":remote"
查看更多
啃猪蹄的小仙女
3楼-- · 2019-05-07 20:45

To answer your question, Yes. Your application will be terminated if a service within your application crashes.
To provide a solution, don't let the service crash. I believe you can prevent a service from crashing if you can prevent whole rest of application. Use try-catch or other programming approaches to handle exceptions. Let us know if anything specific in code needs help for those purposes.

Update: I was assuming you are using "single process model". As the comment suggests, there is more to it. Your process will be terminated if a service within that process crashes. Your application will be terminated if the main thread is running under same process.
By default, Android uses same process throughout your application. In that case, the application will be terminated if any exception occurs (because the process is crashed). You can specify different processes to be run for different activities under manifest (if you "really" need it).
See Process and thread for more details on how to do it.

查看更多
登录 后发表回答