简体   繁体   中英

Is wakelock needed in broadcast receiver for ACTION_SCREEN_OFF

I'm registering a broadcast receiver that will be activated when the screen is turned off. The receiver performs a short, synchronous operation in OnReceive. This operation must happen in my app before the device sleeps.

My question: do I need to acquire a wakelock here, or is it guaranteed that Android will let my receiver finish the OnReceive method? My code takes a few milliseconds to run, but let's say it takes 1 second for the sake of discussion.

I'm using this code to register the receiver:

 BroadcastReceiver receiver = new MyReceiver();
 IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_OFF);
 context.registerReceiver(receiver, filter);

I couldn't find an answer in the documentation or by searching, so I would appreciate any help, thank you!

Yes device might sleep. Newaz why are you taking a chance? Better aquire a Partial wakelock and release it at the end of onReceive.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM