Android服务崩溃行为
我对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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在回答您的问题之前,我想解决一些可能引起您困惑的问题。至少在这种情况下,“应用程序”或“应用程序”一词不是一个明确的术语。因此,询问“应用程序会崩溃”是否有些混乱。由于应用程序可以具有多个进程(通过使用
Android:Process
清单属性)和多个组件,因此说应用程序崩溃是没有意义的。相反,由于未知的例外,过程可能会崩溃。此外,作为应用程序一部分的过程可能会崩溃,但这并不一定会转化为“应用程序”崩溃。另外,对于所有答案,该应用程序是在前景还是背景中运行,都无关紧要。注意:这里的使用“背景”和“前景”也不明确。这是Android文档使用“背景”和“前景”一词的结果。涉及线程,组件生命周期,用户可见性,服务前景/背景状态等
由于服务和活动在单独的过程中,因此一个过程中的崩溃不应影响另一个过程(除非两个过程以某种方式相互通信)。
仅使用Android文档使用的语言,我将重新提出一个问题:
这个问题的答案取决于其他客户是否绑定到服务。如果服务抛出未熟悉的异常(从而崩溃了服务的过程),并且没有其他客户绑定到服务(即只有活动绑定到服务),则该服务将不会重新启动。 ,并自动反弹到以前的客户(这不再存在,因为活动不再存在)
这个问题的答案是肯定的。该服务的过程和服务将被重新创建,并自动反弹到活动以及以前绑定到该服务的任何其他客户。
有关服务寿命周期的更多信息,请参见服务指南, 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.
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).
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).
Just to use the language that the Android documentation uses, I am going to rephrase the question:
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)
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.