我需要在我的BroadcastReceiver唤醒锁,如果我没有启动的服务或活动? [英] do I need a wake lock in my broadcastreceiver if I'm not starting a service or an activity?

查看:406
本文介绍了我需要在我的BroadcastReceiver唤醒锁,如果我没有启动的服务或活动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个调用报警(计划与AlarmManager)的BroadcastReceiver。在这个接收器,我只能查询一个寄存器从数据库中,并推出了通知。我readed,当一个服务或活动从广播接收器启动时需要一个唤醒锁,但是,我需要一个唤醒锁,如果我只是想显示notificacion(通知面板)?

解决方案
  

在此接收器,我只查询一个寄存器从数据库中,并推出了通知。

不要做数据库的I / O主应用程序线程上。

  

我readed,当一个服务或活动从广播接收器启动时需要一个唤醒锁,但是,我需要一个唤醒锁,如果我只是想显示notificacion(通知面板)?

在一般情况下,不,你不会需要一个 WakeLock 的BroadcastReceiver ,即使该通过调用一个 _WAKEUP 报警。 AlarmManager 在这种情况下,可以保证它将器件保持清醒利用自身 WakeLock

然而,同样,在这种情况下,你真的不应该做数据库I / O主应用程序线程上,和的onReceive()被称为主应用程序线。这里的正确的模式是您移动查询从数据库中注册,并启动通知给 IntentService ,您的 BroadcastReceiver的<启动/ code>,这样的工作是在后台线程完成。这的的要求 WakeLock ,因为你现在做的工作的onReceive()之外 。我有一个 WakefulIntentService 管理​​该 WakeLock 为您,如果你想使用它。

I have a broadcastreceiver called by an Alarm (scheduled with AlarmManager). In this receiver I'm only querying a register from the database, and launching a notification. I readed that a wake lock is needed when a service or an activity is started from a broadcast receiver, but, do I need a wake lock if I only want to show a notificacion (in the notification panel)?

解决方案

In this receiver I'm only querying a register from the database, and launching a notification.

Do not do database I/O on the main application thread.

I readed that a wake lock is needed when a service or an activity is started from a broadcast receiver, but, do I need a wake lock if I only want to show a notificacion (in the notification panel)?

In general, no, you would not need a WakeLock from a BroadcastReceiver, even one that is invoked via a _WAKEUP alarm. AlarmManager guarantees in this case that it will keep the device awake using its own WakeLock.

However, again, in this case, you really should not be doing database I/O on the main application thread, and onReceive() is called on the main application thread. The proper pattern here is that you move your "querying a register from the database, and launching a notification" to an IntentService, started by your BroadcastReceiver, so that the work is done on a background thread. This will require a WakeLock, as you are now doing work outside of onReceive(). I have a WakefulIntentService that manages the WakeLock for you, if you wish to use it.

这篇关于我需要在我的BroadcastReceiver唤醒锁,如果我没有启动的服务或活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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