作业调度程序与后台服务 [英] Job Scheduler vs Background Service

查看:100
本文介绍了作业调度程序与后台服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有功能A的应用,该应用应每分钟在后台运行。功能A是应用程序应连接到数据库,读取一些数据,然后获取设备的当前位置,并根据它们检查条件,如果条件为true,则应向用户发送状态栏通知,以便用户单击通知将显示应用程序的用户界面,并且会发生某些事情。

此后台任务应每分钟永久运行,无论该应用程序的使用,关闭或终止(例如向我们显示通知的Facebook或Whatsapp)不管它们是否在应用程序堆栈中。)

现在,我已经搜索并发现Android提供了 Job Scheduler 后台服务 AlarmManager Handlers

但是,我对它们的了解越多,对我来说语句就越矛盾。


  1. 关于处理程序我读到它们不存在很长的延迟时间
    ,并且将在系统重启后终止。因此,它们不适合我的任务。

  2. 但是AlarmManager似乎是解决该问题的一个很好的选择,因为如果允许,即使系统重启后它们仍然存在,并且
    可以重新运行该应用程序。但是在 Android文档中,警报
    管理器旨在用于必须在
    特定时间运行的任务(例如闹钟)。但是我的任务必须每分钟运行

  3. 然后有后台服务。这更多地用于诸如我已阅读的后台下载等任务,而不是为了
    做我已经解释过的事情。

  4. JobScheduler似乎不是要永久执行的任务,而是要满足诸如
    空闲或没有网络等特定约束的任务。其中(如果存在
    ,则建议其他方法使用)是否建议用于我在前
    部分解释的任务


解决方案


我有一个应用程序,该应用程序的功能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.

  1. About Handlers I have read that they do not exist for long delays and will be terminated after system reboot. So they won't be appropriate for my task.
  2. But AlarmManager seems to be a good candidate for the problem because when permitted they exist even after system reboot and can rerun the app. But in the Android Documentation that the Alarm Manager is intended to be used for tasks that have to be run at a specific time (like the Alarm Clock). But my task has to be run every minute.
  3. Then there is Background Service. This is more for tasks like downloading in the background as I have read and not intended for doing something I have explained.
  4. 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... So which of these (or other ones if they exist) do you recommend to use for the task I explained in the first part

解决方案

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.

这篇关于作业调度程序与后台服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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