我有一个具有功能A的应用,该应用应每分钟在后台运行。功能A是应用程序应连接到数据库,读取一些数据,然后获取设备的当前位置,并根据它们检查条件,如果条件为true,则应向用户发送状态栏通知,以便用户单击通知将显示应用程序的用户界面,并且会发生某些事情。 此后台任务应每分钟永久运行,无论该应用程序的使用,关闭或终止(例如向我们显示通知的Facebook或Whatsapp)不管它们是否在应用程序堆栈中。) 现在,我已经搜索并发现Android提供了 Job Scheduler ,后台服务, AlarmManager 和 Handlers 。 但是,我对它们的了解越多,对我来说语句就越矛盾。
解决方案
我有一个应用程序,该应用程序的功能A应该每分钟在后台运行一次。
由于打ze模式,这种情况不会在数亿运行Android 6.0及更高版本的Android设备上发生(并且可能,应用程序待机状态,具体取决于应用程序的其余部分。)
但是AlarmManager似乎是解决此问题的好方法,因为当允许时,甚至在系统重启后仍然存在
不,他们没有。重新启动后,您需要重新计划使用 AlarmManager 安排的所有警报。
警报Manager旨在用于必须在特定时间运行的任务
AlarmManager 支持重复选项。
此功能更多地用于诸如我已阅读但不打算做某事的后台下载等任务我已经解释了。
A 服务对于您使用的任何解决方案都是必不可少的
JobScheduler似乎不是要永久执行的任务,而是要满足特定约束的任务,例如空闲或没有网络
JobScheduler ,与 AlarmManager ,支持重复作业。
那么,这些(或其他,如果存在)是哪个呢?建议使用我在第一部分中解释过的任务
请不要使用它们,因为您无法在Android 6.0上每分钟运行+一旦设备进入打ze模式,这将在屏幕关闭后一小时之内。取而代之的是,要么重新设计应用程序,使其只需要每天进行几次后台工作,要么就不用费心编写该应用程序了。
I have an app which has a feature A which should run in background every minute. Feature A is that the app should connect to a database, read some data then get the current location of the device and based on them check a condition, if the condition is true it should send a statusbar notification to the user so that when the user clicks on the notification the UI of the app will be displayed and something happens. This background task should run permanently every minute, regardless the app is used, closed, terminated (like facebook or Whatsapp that show us notifications regardless they are in the app stack or not). Now I have searched and have found that Android offers Job Scheduler,Background Service, AlarmManager and Handlers. But the more I read about them the more contradictory the statements appear to me.
解决方案
I have an app which has a feature A which should run in background every minute.
That will not happen on hundreds of millions of Android devices, those running Android 6.0 and higher, due to Doze mode (and, possibly, app standby, depending on the rest of your app).
But AlarmManager seems to be a good candidate for the problem because when permitted they exist even after system reboot
No, they do not. You need to reschedule all alarms scheduled with AlarmManager after a reboot.
the Alarm Manager is intended to be used for tasks that have to be run at a specific time
AlarmManager supports repeating options.
This is more for tasks like downloading in the background as I have read and not intended for doing something I have explained.
A Service will be essential for whatever solution you wind up using.
JobScheduler seems not to be for a task that has to be done in permanently, but for tasks that fulfill a specific constraint like idle, or no network
JobScheduler, as with AlarmManager, supports repeating jobs.
So which of these (or other ones if they exist) do you recommend to use for the task I explained in the first part
Use none of them, as you cannot run things every minute on Android 6.0+ once the device goes into Doze mode, which will be within an hour of the screen turning off. Instead, either redesign the app to only need background work a few times per day, or do not bother writing the app.
更多推荐
作业调度程序与后台服务
发布评论