android-在实时应用程序中在后台运行重复任务

我正在编写一个不断监听和检查传感器(几乎所有传感器)并将该数据保存到设备数据库中的应用程序.

我需要使用该数据每X秒进行一些计算,如果计算检查如此,则抛出一个新事件.
我正在考虑在使用应用程序时请求插入设备(关于电池消耗).

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

我希望继续获取传感器数据并将它们保存到数据库,即使应用程序不在前台……只要用户未停止应用程序,它就应该保存这些值.
一种选择是唤醒锁(PARTIAL_WAKE_LOCK,它保持CPU运行).

我想听听不同的意见.
提前致谢!吉列尔莫.

解决方法:

您可以使用AlarmManager设置重复任务(这是Android设置未来/重复任务的首选方式).为了使计算使用服务(如果您认为计算将变得昂贵,请考虑将其移至单独的工作线程或使用IntentService).

关于唤醒锁(来自AlarmManager参考):

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.

上一篇:java-Android AlarmManager.如何考虑时区之间的过渡以及冬夏两季的过渡?


下一篇:ssh: connect to host localhost port 22: Connection refused 问题