gpt4 book ai didi

android - 关于 WakefulBroadcastReceiver 的困惑

转载 作者:行者123 更新时间:2023-11-29 14:24:19 27 4
gpt4 key购买 nike

我一直在通过此链接研究 WakefulBroadcastReceiver:https://developer.android.com/reference/android/support/v4/content/WakefulBroadcastReceiver.html

我对此没有什么困惑:

  1. 即使设备处于 sleep 模式,此接收器是否能确保您接收到广播? (我认为不,它只是在收到广播后让设备保持唤醒状态,直到调用 completeWakefulIntent() 为止。)
  2. 文档说明了接收器中 Intent 服务的使用,并在工作完成后调用 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);
}
}

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

即使上面的代码 fragment 突出显示,在释放锁之前也有 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

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

只有当您想要确保 CPU 保持 Activity 状态以便服务完成其工作时,您才可以使用 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

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

关于android - 关于 WakefulBroadcastReceiver 的困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32437434/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com