在实时应用程序在后台运行重复任务应用程序、实时、后台、任务

2023-09-05 23:24:39 作者:眼泪↗比↘柠檬酸

可能重复:   Running重复任务的背景上的实时应用

你好!我正在写被连续收听应用程序和检查传感器(几乎所有可用的),并保存这些数据转换成在装置中的数据库。

Hi! I'm writing an application which is continuously listening and checking the sensors (almost all available) and saving that data into the database in the device.

我需要做一些计算每一个X秒与数据,并抛出一个新的事件,如果计算检查是这么说的。 我正在考虑,要求有同时使用(对于电池电量的消耗)的应用程序的设备插入。

I need to make some calculations every X second with that data and throw a new event if the calculations check says so. I'm thinking about requesting to have the device plugged in while using the application (regarding battery drain).

什么是对需要进行计算,并引发该事件的任务的最佳方法呢?计时器?主题? AsynkTask? AlarmManager?另一种方法?

What's the best approach for the task that needs to make the calculations and throw the event? Timer? Threads? AsynkTask? AlarmManager? Another approach?

我想保持获取传感器数据,并将它们保存到数据库,尽管如果应用程序是不是在前台...它应该保存的值,只要该应用程序不是由用户停止。 一个选项是唤醒锁(PARTIAL_WAKE_LOCK,这使CPU运行)。

I want to keep getting sensors data and saving them to the database despite if the application is not on foreground...it should save the values as long as the application is not stopped by the user. One option for that is wake locks (PARTIAL_WAKE_LOCK, which keeps CPU running).

我在想,如果你是在正确的道路上运行,而应用程序是如何检查(想象塔移动路径的几个计将需要决定)将发展虽然不能完全我的情况。 我想听听不同的意见。 提前致谢!吉列尔莫。

I'm wondering how an application checking if you are on the correct way while running (imagine tha moving a few meter of your path would need to be advised) would be developed despite it is not exactly my case. I'd like to hear different opinions. Thanks in advance! Guillermo.

推荐答案

您可以使用 AlarmManager 设置重复任务(这是Android的prefered方式设定未来/重复任务)。为了使计算使用服务(如果你认为计算将是昂贵的,再想想它们移动到一个单独的工作线程或使用 IntentService )。

You can use AlarmManager to setup the repeating tasks (this is the Android prefered way of setting future/repeating tasks). To make the calculations use a Service (if you think calculations are going to be expensive, then think about moving them to a separate worker thread or use IntentService).

关于唤醒锁(从AlarmManager参考):

Regarding the wake lock (from the AlarmManager reference):

报警管理器拥有CPU唤醒   锁定只要报警接收机的   的onReceive()方法正在执行。本   保证手机不会   睡眠,直到完成处理   广播。一旦的onReceive()   返回时,报警管理器版本   这种唤醒锁。这意味着该   手机会在某些情况下,尽快入睡   为您的onReceive()方法完成。   如果您的报警接收器被称为   Context.startService(),有可能   该手机将前入睡   要求的服务被启动。至   prevent这一点,你的BroadcastReceiver   和服务将需要实现一个   独立之后锁定策略,以确保   该手机将继续运行,直到   该服务将变为可用。

The Alarm Manager holds a CPU wake lock as long as the alarm receiver's onReceive() method is executing. This guarantees that the phone will not sleep until you have finished handling the broadcast. Once onReceive() returns, the Alarm Manager releases this wake lock. This means that the phone will in some cases sleep as soon as your onReceive() method completes. If your alarm receiver called Context.startService(), it is possible that the phone will sleep before the requested service is launched. To prevent this, your BroadcastReceiver and Service will need to implement a separate wake lock policy to ensure that the phone continues running until the service becomes available.