Android服务崩溃行为

发布于 2025-02-11 08:06:51 字数 357 浏览 1 评论 0原文

我对Android Service崩溃有一些疑问:

1,一个应用程序在前景中运行;如果活动和服务在相同的过程中运行,则活动不调用服务。如果服务崩溃,应用程序会崩溃吗?

2,在前景中运行一个应用程序,如果活动在一个过程中运行,则该服务在同一应用程序拥有的另一个过程中运行,活动请勿调用服务。如果服务崩溃,应用程序会崩溃吗?

3,一个应用程序在前景中运行;如果活动和服务在相同的过程中运行,则活动通话服务。如果服务崩溃,该服务会重新启动吗?并且应用程序会崩溃吗?

4,一个应用程序在前景中运行;如果活动在一个过程中运行,则该服务在同一应用程序拥有的另一个过程中运行,即活动调用服务。如果服务崩溃了,该服务会重新启动吗?并且应用程序会崩溃吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

葬﹪忆之殇 2025-02-18 08:06:51

在回答您的问题之前,我想解决一些可能引起您困惑的问题。至少在这种情况下,“应用程序”或“应用程序”一词不是一个明确的术语。因此,询问“应用程序会崩溃”是否有些混乱。由于应用程序可以具有多个进程(通过使用Android:Process清单属性)和多个组件,因此说应用程序崩溃是没有意义的。相反,由于未知的例外,过程可能会崩溃。此外,作为应用程序一部分的过程可能会崩溃,但这并不一定会转化为“应用程序”崩溃。

另外,对于所有答案,该应用程序是在前景还是背景中运行,都无关紧要。注意:这里的使用“背景”和“前景”也不明确。这是Android文档使用“背景”和“前景”一词的结果。涉及线程,组件生命周期,用户可见性,服务前景/背景状态等

  1. 。一个重要的(且未记录的)细节是,在Android应用程序中,线程中未知例外的默认行为与常规Java程序中的默认行为不同。默认情况下,在Android应用程序中,如果未涉及线程中的异常,则将崩溃整个过程。而不是仅撞线。因此,无论是在主线程中还是在工作线程中抛出一个例外,它都会崩溃该过程(因此服务和活动)。

  2. 由于服务和活动在单独的过程中,因此一个过程中的崩溃不应影响另一个过程(除非两个过程以某种方式相互通信)。

  3. 仅使用Android文档使用的语言,我将重新提出一个问题:

活动和服务正在同一过程中运行。活动是绑定(通过将bindservice(...))调用。如果服务的流程崩溃,服务将重新启动?

这个问题的答案取决于其他客户是否绑定到服务。如果服务抛出未熟悉的异常(从而崩溃了服务的过程),并且没有其他客户绑定到服务(即只有活动绑定到服务),则该服务将不会重新启动。 ,并自动反弹到以前的客户(这不再存在,因为活动不再存在)

  1. 但是,如果其他客户绑定到服务,那么服务的过程和服务将被重新创建 重新设计使用与Android文档一致的语言:

活动和服务正在以不同的进程运行。活动是绑定(通过将bindservice(...))调用。如果服务的流程崩溃,服务将重新启动?

这个问题的答案是肯定的。该服务的过程和服务将被重新创建,并自动反弹到活动以及以前绑定到该服务的任何其他客户。

有关服务寿命周期的更多信息,请参见服务指南 service doc 界服务指南 ServiceConnection doc

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文