我的Android应用程序活动绑定到服务,该服务连接到BLE外设并接收数据。直到用户按下活动上的按钮以断开与外围设备的连接并停止服务为止。
有时在锁定屏幕的情况下运行约45分钟后,活动和服务将终止。我的结论是,Android 8.1比以前的操作系统更具侵略性,因此我将后台服务更改为前台服务,并确保在连接外围设备后就停止扫描。
这没有用,并且我的应用程序有时会在后台被杀死,我认为这不是一个未捕获的异常。我已经阅读了有关服务和应用程序生命周期的Android文档,并在生命周期回调中添加了一些调试日志,但这没有帮助。我已经在运行Android 8.1的Huawei Y5和Nokia 8上观察到了这种行为-我不记得这是我不再拥有的旧手机上的问题。
我有一些关于行为的问题,没有明确记录下来,这可能有助于我弄清这个问题。感谢您的帮助!
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.
答案 0 :(得分:1)
Android杀死您的活动有多种原因。因此,在活动中设置蓝牙不是一个好主意。在单独的类或您的Application类中进行设置,并使用其他方式将数据发送到您的活动中,例如广播。这样,即使您的活动被杀死或重新创建,它也将继续起作用。
您的应用程序确实被杀死了还是仅仅是您的Activiry被杀死了?