【问题标题】:Confusion about WakefulBroadcastReceiver关于 WakefulBroadcastReceiver 的困惑
【发布时间】:2015-09-07 11:20:06
【问题描述】:

我一直在通过这个链接学习 WakefulBroadcastReceiver:https://developer.android.com/reference/android/support/v4/content/WakefulBroadcastReceiver.html

我对此有一些困惑:

  1. 此接收器是否确保即使设备处于睡眠模式也能接收到广播? (我认为不,它只是在接收到广播后保持设备唤醒,直到调用 completeWakefulIntent()。)
  2. 文档说明了在接收器中使用意图服务,并在工作完成后调用 completeWakefulIntent。

代码:

import android.app.IntentService;
import android.content.Intent;
import android.os.SystemClock;
import android.util.Log;

public class SimpleWakefulService extends IntentService {
    public SimpleWakefulService() {
        super("SimpleWakefulService");
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        // At this point SimpleWakefulReceiver is still holding a wake lock
        // for us.  We can do whatever we need to here and then tell it that
        // it can release the wakelock.  This sample just does some slow work,
        // but more complicated implementations could take their own wake
        // lock here before releasing the receiver's.
        //
        // Note that when using this approach you should be aware that if your
        // service gets killed and restarted while in the middle of such work
        // (so the Intent gets re-delivered to perform the work again), it will
        // at that point no longer be holding a wake lock since we are depending
        // on SimpleWakefulReceiver to that for us.  If this is a concern, you can
        // acquire a separate wake lock here.
        for (int i=0; i<5; i++) {
            Log.i("SimpleWakefulReceiver", "Running service " + (i+1)
                    + "/5 @ " + SystemClock.elapsedRealtime());
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
            }
        }
        Log.i("SimpleWakefulReceiver", "Completed service @ " + SystemClock.elapsedRealtime());
        SimpleWakefulReceiver.completeWakefulIntent(intent);
    }
}

现在,正如文档所说,这个接收器持有唤醒锁,我非常怀疑从接收器启动类似意图服务的东西是否是一种好习惯。这将阻止接收器释放唤醒锁并消耗大量电池,因为服务通常用于长时间操作。

即使上面的代码 sn-p 突出显示,在释放锁之前也有 25 秒的延迟。这是使用此接收器的正确方法还是应该仅用于短期操作?任何帮助表示赞赏。

【问题讨论】:

  • 我不是一个完美的人,说它是好是坏,但它很完美
  • 基本上没有对接收器本身的引用......它只是开始为意图持有锁......并且 static 方法释放锁......
  • @Selvin 即使没有参考(我的意思是你建议的静态方法),仍然有一个唤醒锁,这无论如何都会耗尽电池?
  • 你能不能详细点,没明白你的意思 ...为什么... WakefulBroadcastReceiver的来源非常简单... 还有一个保持唤醒锁,这无论如何都会耗尽电池? ...这就是为什么有 completeWakefulIntent 方法
  • @Selvin 间接我的问题是:在长时间运行的服务中是否需要唤醒锁,如果它不会耗尽电池电量?您可以根据代码中的此注释忽略接收器:此时 SimpleWakefulReceiver 仍在为我们持有唤醒锁。我们可以在这里做任何我们需要做的事情,然后告诉它它可以释放唤醒锁。这个示例只是做了一些缓慢的工作,但更复杂的实现可以在释放接收器之前在这里使用自己的唤醒锁。

标签: android broadcastreceiver android-wake-lock


【解决方案1】:

此接收器是否确保即使设备处于睡眠模式也能接收到广播?

没有。

(我认为不,它只是在接收到广播后保持设备唤醒,直到调用 completeWakefulIntent()。)

正确。

我非常怀疑从接收方启动意图服务之类的东西是否是一种好习惯

是的,这是一个很好的做法。这就是重点。

这会阻止接收器释放唤醒锁

唤醒锁保存在 static 数据成员中,这就是 static 方法 (completeWakefulWork()) 能够释放它的原因。

因为服务通常用于长时间操作

“长”的定义各不相同。我开始使用WakefulBroadcastReceiver(或我的WakefulIntentService 前任)来处理可能超过10 秒的任何事情。但是,任何形式的IntentService 实际上只为事务性工作而设计(例如,下载大文件),因此它不适合服务可能需要无限期运行的情况(例如,只要用户想要与某个聊天服务器交谈)。

仅当您希望确保 CPU 保持活动状态以使服务完成其工作时,才使用 WakefulBroadcastReceiver。否则,您直接使用IntentService。

这是使用此接收器的正确方法还是应该仅用于短期操作?

25 秒是使用WakefulBroadcastReceiver 及其关联的IntentService 的完全合理的时间范围。

但更复杂的实现可以在释放接收器之前在此处使用自己的唤醒锁

当然可以,尽管目前还不清楚这会给您带来什么。唤醒锁是唤醒锁。服务拥有的唤醒锁对电池的影响与接收者拥有的唤醒锁完全相同。

【讨论】:

  • 谢谢!简洁明了。我现在明白了很多。
  • 你能举一些什么时候使用 WakefulBroadcastReceiver 的例子吗?警报管理器应用程序使用它吗?
  • @androiddeveloper:“你能举一些什么时候使用 WakefulBroadcastReceiver 的例子吗?” -- 任何时候你的代码在后台被唤醒,你都需要WakeLock,除非另有说明,因为你不知道你会醒来多久。 WakefulBroadcastReceiver 专为与AlarmManager 一起使用而设计,因为AlarmManager 保证让设备保持唤醒足够长的时间以供onReceive() 完成。
  • @CommonsWare 所以除非闹钟应用程序不做像从互联网上加载东西这样的长任务,否则它不需要使用它,对吧?
  • @DonBox:“为什么这不是一个好习惯?” ——因为不是我写的,我不知道你为什么在这里评论。这是一个好习惯,正如我的回答所涵盖的那样。
猜你喜欢
  • 2019-05-17
  • 2020-03-06
  • 2016-03-16
  • 2013-07-27
  • 1970-01-01
  • 2012-09-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多