PowerManager.PARTIAL_WAKE_LOCK android [英] PowerManager.PARTIAL_WAKE_LOCK android
问题描述
我很困惑是否要获得这个唤醒锁.例如.我有从 BroadcastReceiever
(CONNECTIVITY_CHANGE、BOOT_COMPLETED 等)异步的 onReceive()
调用的这种类型的代码,即我正在启动一个 IntentService
来自 onReceive()
,它执行繁重的工作.
I am very confused whether to acquire this wakelock. E.g. I have this type of code that is called from onReceive()
of a BroadcastReceiever
(CONNECTIVITY_CHANGE, BOOT_COMPLETED etc) asynchronously i.e. I am launching an IntentService
from onReceive()
which performs heavy lifting.
private static void insertInDatabase(Context context /*, some data to be inserted in database*/) {
Database helper = Database.getInstance(context);
PowerManager pm = (PowerManager) context
.getSystemService(Context.POWER_SERVICE);
final WakeLock wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, wakelockName);
wakeLock.acquire();
try {
SQLiteDatabase db = helper.getWritableDatabase();
ContentValues cv = new ContentValues();
// insert data in database here
} finally {
wakeLock.release();
}
}
这种情况是否适合收购 PowerManager.PARTIAL_WAKE_LOCK
?
Is this scenario the right candidate to acquire PowerManager.PARTIAL_WAKE_LOCK
?
推荐答案
@paha 的回答忽略了一个重点:IntentService 是不够的.在 onReceive()
结束和 IntentService 启动之间,手机可能会再次入睡.你需要一个(静态)锁来弥补这个差距——这是在 Mark Murpphy 的 WakefulIntentService中实现的代码>
The answer by @paha misses an important point : IntentService is not enough. Between onReceive()
ends and the IntentService is started the phone might fall asleep again. You need a (static) lock to bridge this gap - this is implemented in Mark Murpphy's WakefulIntentService
因此保留 AlarmManager 和接收器,但从您的 onReceive()
启动 WakefulIntentService
.
So keep the AlarmManager and receiver but launch a WakefulIntentService
from your onReceive()
.
见:
这篇关于PowerManager.PARTIAL_WAKE_LOCK android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!