Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If Android service crashes will it bring down App on which it is running?

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.

like image 987
shivakumar Avatar asked Jan 15 '15 12:01

shivakumar


People also ask

What happens when an app crashes Android?

This usually occurs when your Wi-Fi or cellular data is slow or unstable, causing apps to malfunction. Another reason for Android apps crashing can be a lack of storage space in your device. This can occur when you overload your device's internal memory with heavy apps.

What causes app crashes on Android?

Why are apps on my Android crashing? Apps on Android can crash because of low storage space, too many apps running simultaneously, a weak internet connection, or not having the proper app updates installed.

What factors can cause an app to crash?

An Android app crashes whenever there's an unexpected exit caused by an unhandled exception or signal. An app that is written using Java or Kotlin crashes if it throws an unhandled exception, represented by the Throwable class.


2 Answers

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.

like image 76
VipulKumar Avatar answered Sep 19 '22 03:09

VipulKumar


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"
like image 30
David Wasser Avatar answered Sep 18 '22 03:09

David Wasser