Correct Way to Register for SCREEN_OFF and SCREEN_ON events

74 Views Asked by At

So I recently discovered that SCREEN_OFF and SCREEN_ON actions can't be received by registering them in an application's manifest (no idea why), which requires me to register programmatically instead.

However, I'm not sure where would be the best place to do this? Ideally I would like to just register by BroadcastReceiver to handle these events just like I handle everything else, but where should I actually perform the registration? Is there another action I could register to that would be a better place for me to perform such setup actions?

Currently I'm doing this by triggering a broadcast in the main activity when opened, that the receiver can then receive, and attempt to register itself for screen events. But this will only work if the application has been run at least once.

1

There are 1 best solutions below

2
Ed_ On BEST ANSWER

You could register a BOOT_COMPLETED receiver and use that to run the code that would listen to screen off/on should the device get rebooted.

Note you will need the RECEIVE_BOOT_COMPLETED permission.