0

I'm developing an Android app that reads data from a Bluetooth device and saves it to disk (and perhaps send some data to a server). The Bluetooth device is a wearable with a heart rate sensor and a bunch of other sensors. It sends data to the Android app every second or so.

The app works fine when in foreground, but when in background, it gets killed after a few minutes (usually 15 to 30 min).

I understand the concepts of foreground and background services and it seems like what I need here is a foreground service (correct me if I'm wrong) to make sure the process doesn't get killed. It's a pain however because I'll have to move most of my app's classes in remote objects in the foreground service classes.

However! I noticed that just creating a simple foreground service and binding my main activity to it makes it so that the activity doesn't get killed as fast, as a matter of fact I tested it for an hour and a half and everything was still running smoothly in the main activity process.

Could that just be the solution I'm looking for? Or do I really have to move most of my classes in remote objects in a more complex foreground service?

4

1 回答 1

0

您使用 ForeGround 服务实现它很好。

您需要做的下一件事是创建一个 BroadCastReceiver,它将调用您的前台服务并从 MainActivity 的 OnDestroy() 方法调用 BroadcastReceiver。

于 2019-05-17T04:39:24.953 回答