我仍然可以在Android 8.0中使用数据记录后台服务吗? [英] Can I still have a data logging background service in Android 8.0?

查看:98
本文介绍了我仍然可以在Android 8.0中使用数据记录后台服务吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序依赖于在后台服务中每秒记录一次电池温度以及其他几个传感器变量,该服务可以从主要活动中启动,并继续在后台运行直到停止(通过主服务中的另一个按钮)活动).在Android 7.0及以下版本中,此方法运行良好,但在阅读Android 8.0时,它似乎严重限制了后台服务.我仍然可以像现在这样进行数据记录,还是不再可能?

解决方案

如果您的用例是在此处理过程中将应用程序置于前台, 根据文档,应该没有问题,因为如果您的应用程序不在Foreground中或没有前台应用程序连接到该应用程序,则会应用对后台服务的限制

您的应用进入后台后

X分钟(根据我的观察,大约是1-2分钟),后台服务的所有限制将生效,并且您的服务将被停止,就像您已调用Service.stopSelf()

您应避免连续进行操作,因为如果打算在后台进行操作,则会影响设备的电池寿命.您可以使用JobScheduler/AlarmManager定期执行日志记录.

如果仍然要继续,则可以使服务ForegroudService并执行操作,但是如果系统认为您的任务占用大量CPU,则此方法也将无法工作

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?

解决方案

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

这篇关于我仍然可以在Android 8.0中使用数据记录后台服务吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆