假设我使用 BroadcastReceiver 来接收唤醒手机的事件( Intent ),即使它处于深度 sleep 模式(例如套接字上的传入数据包或传入的文本消息)。然后我将收到的数据转发给 IntentService 进行处理。我应该使用 WakeLock 吗?
如果我不使用唤醒锁,我能否确保设备不会进入休眠模式,直到我的服务的 Intent 队列为空(因此服务停止)? (假设处理可能需要很长时间)。
如果需要 WakeLock,那么应该在何时何地创建和获取它,以及何时释放它?我想在服务的 Intent 队列为空后立即释放 WakeLock。
提前致谢
最佳答案
不能保证设备保持清醒状态足够长的时间来完成 Intent 队列。如果及时处理 Intent 真的很重要,最好的办法是在 onHandleIntent
中做这样的事情:
mWakeLock.acquire(); // mWakeLock should be set to be reference counted
// (do work)
mWakeLock.acquire(1000); // hopefully long enough to get to the next item
mWakeLock.release();
关于android - 我应该在 IntentService 中使用 WakeLock 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6090798/