且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Firebase childeventlistener 的后台服务在几分钟后无法工作

更新时间:2023-01-24 17:56:48

第一个回答:

您的服务正在被终止,因为您可能正在尝试执行 Google/Android 操作系统明确不希望发生的事情.这是 SDK 文档的一部分,然后我会解释:

Your service is being killed because you might be attempting to do something that Google/Android OS explicitly doesn't want to happen. Here's a cut from the SDK docs and then I'll explain:

(来自服务生命周期)因为通常只有少数进程对用户可见,这意味着除非内存不足,否则不应终止服务.然而,由于用户并不直接知道后台服务,在这种状态下它被认为是一个有效的候选者,你应该为这种情况的发生做好准备.特别是,如果长时间运行的服务保持启动时间足够长,它们将越来越有可能被终止,并且保证会被终止(并在适当的情况下重新启动).

(From Service Lifecycle) Because only a few processes are generally visible to the user, this means that the service should not be killed except in low memory conditions. However, since the user is not directly aware of a background service, in that state it is considered a valid candidate to kill, and you should be prepared for this to happen. In particular, long-running services will be increasingly likely to kill and are guaranteed to be killed (and restarted if appropriate) if they remain started long enough.

你看,他们实际上试图确保用户不会有数百个 SpyServices 永久运行,占用资源或其他任何东西.你怎么能避免这种情况?答案就在那里……显示某种类型的通知,即使是简单地说服务正在运行"的通知也会防止服务被破坏.当然,如果您实际上是在尝试监视"用户,那么发出间谍服务正在运行的通知并不是一个好主意.如果您想继续使用此模式,请尝试使用不可见"通知图标和非打印文本.如果用户正在查看通知,他们可能看不到它,或者认为这只是一个小故障.

You see, they're actually trying to make sure that the user doesn't have hundreds of SpyServices running perpetually, hogging resources or whatever. How can you avoid this? The answer is right there...show some type of Notification, even one that simply says 'service running' will keep the service from being destroyed. Of course, if you're actually trying to 'spy' on the user, putting a notification that the spy service is running isn't a good idea. If you want to proceed with this pattern, try an 'invisible' notification icon and non-printing text. If the user's looking at the notifications, they might not see it, or think it's just a glitch.

第二个答案:

切换到更事件驱动"的设计.我假设您能够捕捉到启动时"、呼叫接收"和其他消息,因此请为指示手机使用情况的事件注册接收器,这样您就可以轻松地将多个 10-15 分钟的内容拼接在一起段以获得几乎完全覆盖.

Switch to a more 'event-driven' design. I'm assuming that you're able to catch the 'on-boot', 'call-received' and other messages, so register receivers for events that would indicate handset usage, that way you could easily stitch together multiple 10-15 minute segments to get almost full coverage.

我的目标是:

  • 电源连接/断开
  • WiFi 状态更改
  • 屏幕背光开/关和/或屏幕锁定状态已更改.

第三个答案:

看看绑定"模式.如果您可以让任何活动绑定"到服务,只要它被绑定,它就保证不会被杀死.如果您将START_STICKY"与绑定模式结合使用,您将能够在绑定释放后继续运行一段时间.

Take a look at the 'binding' pattern. If you can get any of the activities to 'bind' to the service, its guaranteed NOT to be killed as long as it's bound. If you combine the 'START_STICKY' with the binding pattern, you'll be able to keep running for some period after the binding is released.