关于 WakefulBroadcastReceiver 的困惑

Confusion about WakefulBroadcastReceiver

我一直在研究 WakefulBroadcastReceiver link : 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);
    }
}

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

即使上面的代码片段突出显示,在释放锁之前也有 25 秒的延迟。这是使用此接收器的正确方法还是应该仅用于短时间操作?感谢任何帮助。

Does this receiver ensures that you'll receive the broadcast even when the device is in sleep mode?

没有

(I think no, it just keeps the device awake after receiving the broadcast until a call to completeWakefulIntent() is made.)

正确。

I highly doubt that it's a good practice to start something like an intent service from the receiver

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

This would prevent the receiver from releasing the wake lock

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

as services are generally used for long operations

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

只有在您想要确保 CPU 保持活动状态以便服务完成其工作时才使用 WakefulBroadcastReceiver。否则,你直接使用IntentService

Is this the right way to use this receiver or should it be used only for short operations?

25 秒是使用 WakefulBroadcastReceiver 及其关联的 IntentService.

的完全合理的时间范围

but more complicated implementations could take their own wake lock here before releasing the receiver's

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