Can I still have a data logging background service in Android 8.0?

非 Y 不嫁゛ 提交于 2019-12-17 22:28:53

问题


My app relies on logging the battery temperature, as well as several other sensor variables, every second in a background service that can be started from the main activity and continues running in the background until it's stopped (by another button in the main activity). This works fine in Android 7.0 and below, but reading into Android 8.0, it looks like it limits background services pretty severely. Can I still do this data logging that I currently do, or is that no longer possible?


回答1:


If your usecase is to keep the app in foreground during this processing then, based on the documentation, there shouldn't be any problem, since the restrictions for background service is applied if your app is not in Foreground or no foreground app is connected to the app

X minutes (based on my observations its around 1 - 2 minutes) after your app enters in background all the restrictions for background service will kick in and your Service will be stopped as if you have called Service.stopSelf()

You should avoid doing it continuously, since it will impact battery life of the device, if are intending to do in background. You can perform logging periodically using JobScheduler/AlarmManager.

If you still want to proceed, you can make the service ForegroudService and perform the operation but if system considers your task as CPU intensive then this approach wont' work either



来源:https://stackoverflow.com/questions/49566426/can-i-still-have-a-data-logging-background-service-in-android-8-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!