Kevin Ding
Kevin Ding

Reputation: 814

android service crash behavior

I have some questions about android service crash:

1、A app is running in the foreground; If activity and service run in the same process, activity don't call service . if service crashes , will the app crash?

2、A app is running in the foreground;If activity run in one process, the service run in the another process owned by the same app, activity don't call service. if the service crashes, will the app crash?

3、A app is running in the foreground; If activity and service run in the same process, activity call service . if service crashes , will the service be restarted?, and will the app crash?

4、A app is running in the foreground;If activity run in one process, the service run in the another process owned by the same app, activity call service. if the service crashes, will the service be restarted?, and will the app crash?

Upvotes: 0

Views: 915

Answers (1)

user19309143
user19309143

Reputation:

Before I answer your questions, I want to address something which might be causing you confusion. The word "app" or "application" is not a well-defined term, at least in this context. So asking if the "app will crash" is a bit confusing. Because an application can have multiple processes (through the use of the android:process manifest attribute) and multiple components, it doesn't make sense to say an app crashes. Rather, a process can crash because of an uncaught exception. Moreover, a process which is part of an application can crash, but this doesn't necessarily translate into the "app" crashing.

Also, for all the answers, it does not matter whether the app is running in the foreground or background. Note: the use of the words "background" and "foreground" are also not well-defined here. This is a result of the Android documentation using the words "background" and "foreground" in different ways; either referring to threads, component life cycles, user visibility, service foreground/background state, etc.

  1. Yes, an uncaught exception (originating from either the Service or the Activity) will crash the process. An important (and non-documented) detail is that the default behavior for an uncaught exception in a thread is different in an Android application than in a regular Java program. By default in an Android application, if an exception in a thread is uncaught, then it will crash the entire process; as opposed to crashing only the thread. So regardless of whether an exception was thrown in the main thread or in a worker thread, it will crash the process (and therefore both the service and activity).

  2. Because the service and activity are in separate processes, a crash in one process should not effect the other (unless the 2 processes are communicating with each other in some way).

  3. Just to use the language that the Android documentation uses, I am going to rephrase the question:

An Activity and Service are running in the same process. The Activity is bound (by calling bindService(...)) to the Service. If the Service's process crashes, will the Service restart?

The answer to this question depends on whether other clients are bound to the Service. If the Service throws an uncaught exception (thereby crashing the Service's process) and no other clients are bound to the Service (i.e. only the Activity is bound to the Service), then the Service will not be restarted. However, if other clients are bound to the Service, then the Service's process and the Service will be re-created and automatically rebound to the previous clients (this does not include the Activity in the question, as the Activity no longer exists)

  1. Again just rephrasing to use language consistent with Android documentation:

An Activity and Service are running in different processes. The Activity is bound (by calling bindService(...)) to the Service. If the Service's process crashes, will the Service restart?

The answer to this question is yes. The Service's process and the Service will be re-created and automatically rebound to the Activity and any other clients that were previously bound to the service.

For more information about Service life cycles, see the Service Guide, Service doc, Bound Service Guide, and the ServiceConnection doc.

Upvotes: 1

Related Questions