当应用程序在 android 中更新时,后台服务会发生什么情况?

What happens to background service when the app is updated in android?

假设我启动了一个重复的后台服务,该服务在首次启动应用程序和启动时声明。当我提供应用程序更新时会发生什么。那个后台服务会被杀死吗? 用户是否需要再次打开应用程序以再次注册后台服务,或者应用程序会在更新时获得一些回调?

Edit-1:作为答案之一,如果必须重新启动应用程序以启动服务,那么警报应用程序如何在更新后正常工作而无需重新启动(我相信它使用后台服务来启动警报)?

Will that background service be killed?

会被杀死

Will user have to open the app again to register the background service again or app will get some callback on update?

视情况而定。基本上它需要用户 activity 因为应用程序不会在更新后自动重新启动。但是如果你的目标是 API 12 或更高(现在你应该这样做),你可以尝试使用 ACTION_MY_PACKAGE_REPLACED 广播。根据文档:

Broadcast Action: A new version of your application has been installed over an existing one. This is only sent to the application that was replaced. It does not contain any additional data; to receive it, just use an intent filter for this action.

所以你可以在 BroadcastReceiver 收到这个广播后触发一些东西。

该服务将被终止,需要重新启动。

服务不在单独的进程上 运行。 Service 对象本身并不意味着它在自己的进程中 运行ning;除非另有说明,否则它 运行 与它所属的应用程序处于同一进程中。

所以当应用程序更新时,应用程序被发送到停止状态。

你可以测试一下。 从 google play store 启动应用程序更新(它有一项服务,例如 Whatsapp)。 打开应用程序并等待它完成。它停止了。您可以检查内部 运行ning 进程。将 phone 连接到 DDMS。检查进程。