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.
相关问题
- How can I create this custom Bottom Navigation on
- Bottom Navigation View gets Shrink Down
- How to make that the snackbar action button be sho
- Listening to outgoing sms not working android
- How to create Circular view on android wear?
One option is to run your
Service
in a separate process from the rest of your app. If yourService
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 theService
has crashed. Of course, your app can attempt to restart theService
and/or take other measures.To do this, just add this to the
<service>
declaration in your manifest: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.