I have an android target tablet that will be deployed to a vehicle. The tablet will be connected to two serial devices and needs to have a process on board that will send periodic readings,once every 4 seconds from one serial device and every other minute from the other serial device, back to a web server via a REST call. The tablet will also run a web app, related to but not connected to the process monitoring the serial ports. The serial port process has to be running ALL the time and meeting the SLA of sending data every 3 seconds.
As an android noob my readings so far suggest that since android 8 the battery optimization improvements from google have largely led to background services loosing their ability to be long running and background. I have seen several approaches (look like hacks) to circumvent this. At the tablet level I think there is a real use case and need for a long running daemon.
My question is to appeal to those with experience as to the best approach here. Am I better to use the Service API in java and build a background service or is the best approach to deploy an NDK C++ exe?
The Service API is not part of the NDK API so I assume I have posix threads to work with in C++? Do I still deploy an APK? Rooting the device is NOT an option.
I cannot find use cases that match what I am trying to do, and as an android new comer, the hope is that people who have spend years using the various API's I refer to will shed some hard won experience on this issue saving me significant effort and time.
If the process doesn't need to be guaranteed (in the case of the device going offline for example) but is not deferrable, a foreground service would work well.
Foreground services are capable of achieving what you would want probably, but they will display a running notification for as long as they are active. Swiping the notification would kill the service. Power cycling would as well. Still, it is a good solution, provided the user is cognizant of the importance that it remains running.
Regardless of whether you use foreground services or some other solution, your app may request to be ignored by battery optimizations. Source.
An example of how you can do this:
and ensure that
is in your app's manifest. This permission is not considered
dangerous.