我应该使用AlarmManager或处理程序? [英] Should I use AlarmManager or Handler?

查看:144
本文介绍了我应该使用AlarmManager或处理程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一个应用程序,不断轮询设备的传感器每隔一段时间应该记下一些统计数据到一个文件中。这可能是一样快,一旦第二或慢每分钟一次。我应该使用处理程序的 postDelayed()方法或者安排其与 AlarmManager

I'm writing an app that constantly polls the device's sensors and every so often should write down some statistics to a file. This could be as fast as once a second or as slow once a minute. Should I use Handler's postDelayed()method or just schedule it with the AlarmManager?

推荐答案

我会说,这取决于轮询间隔。我想这是相当低的,你的情况(约几秒钟),所以你应该去处理程序的方式,或通过使用Timer类。

I'd say that it depends on the polling interval. I guess it's quite low in your case (around a few secs), so you should go the Handler way, or by using the Timer class.

AlarmManger是一个更高层次的服务,它涉及到一个更大的开销来处理这个用例。当报警触发,你需要BroadcastReceivers处理它。这意味着每次你处理这些报警一次,你需要注册听众对你感兴趣的传感器,这是非常低效的恕我直言。

AlarmManger is a much higher level service and it involves a larger overhead to handle this use case. When an alarm triggers, you need to handle it with BroadcastReceivers. This means that every time you handle one of these alarm, you needs to register listeners for the sensors you're interested in, which is immensely inefficient imho.

这篇关于我应该使用AlarmManager或处理程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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