我的Android App活动与服务结合,该服务连接到BLE外围并接收数据。它要执行此操作,直到用户按下活动上的按钮以与外围断开并停止服务为止。
有时用屏幕锁定约45分钟后,活动和服务将终止。我的结论是,Android 8.1比以前的操作系统更具侵略性,因此我将背景服务更改为前景服务,并确保一旦连接了外围,我就停止扫描。
这不起作用,时不时地我的应用在后台被杀死,我认为这不是一个未被发现的例外。我已经阅读了有关服务和应用程序生命周期的Android文档,并将一些调试日志添加到生命周期回调中,这对此无济于事。我已经在华为Y5和诺基亚8运行Android 8.1上观察到了这种行为 - 我不记得这是我不再遇到的旧手机上的问题。
我有一些关于行为的问题,这些问题尚未清楚地记录在可能有助于我弄清楚这个问题的情况下。感谢您的帮助!
1. If an activity is killed in the background and it is bound to a service, will that kill the service?
2. If a service is killed in the background, will that kill my activity
3. If there are multiple services running and one consumes too many resources, does the OS kill everything, or just the misbehaving service?
4. What sort of things makes an activity or service a target for the OS? Holding on to wakelocks permanently? BLE scanning? Receiving too many BLE packets? Uploading too much data? Any insight here about what might be happening under the hood is appreciated.
5. Does having the phone on charge prevent the OS from killing apps? I believe it occurs in my case regardless.
杀死Android的原因有几个。因此,在活动中设置蓝牙不是一个好主意。将其设置为单独的类或应用程序类,并使用其他方式将数据发送到您的活动,例如广播。这样,即使您的活动被杀死或重新创建,它也将继续工作。
您的申请真的被杀死了,还是仅仅是您被杀死的活动?