Is there any way to find out that the android device screen is on or not without broadcast receivers? I want to make minute interval updates on device via service that is invoked by alarm manager. I also want to preserve battery life. So the update service will run if the device screen is on.
i found a solution to my problem with this code:
PowerManager powermanager;
powermanager = (PowerManager) this.getSystemService(Context.POWER_SERVICE);
if (powermanager.isScreenOn())
{
...
}
You can consider another approach to the problem:
Using Broadcast Receivers, set your repeating alarm when the screen is turned on. Then, when the screen is turned off, cancel your alarm.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With