PowerManager.PARTIAL_WAKE_LOCK机器人 [英] PowerManager.PARTIAL_WAKE_LOCK android
问题描述
我很困惑是否获取此wakelock。例如。我有这种类型的code,它是从的onReceive称为()
的 BroadcastReceiever
(CONNECTIVITY_CHANGE,BOOT_COMPLETED等)异步也就是我发动 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
?
推荐答案
由@paha答案忽略了一个很重要的一点:IntentService是不够的。在的onReceive()
结束,IntentService启动手机可能会再次入睡。你需要一个(静态)锁弥合这一差距 - 这是马克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和接收器,但推出的 WakefulIntentService
的从你的的onReceive()
。
So keep the AlarmManager and receiver but launch a WakefulIntentService
from your onReceive()
.
请参阅:
- <一个href="http://stackoverflow.com/questions/12417541/android-deep-sleep-and-wake-locks/16268293#16268293">Android深睡眠和唤醒锁
- <一个href="http://stackoverflow.com/questions/17471028/powermanager-wakelock-not-waking-device-up-from-service/17496029#17496029">PowerManager wakelock不是从服务中唤醒设备最多
- Android deep sleep and wake locks
- PowerManager wakelock not waking device up from service
这篇关于PowerManager.PARTIAL_WAKE_LOCK机器人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!