Programmatically turn screen on in android

后端 未结 3 1189
日久生厌
日久生厌 2020-12-14 23:40

I am developing an alarm application. From the main activity i set the alarm using broadcast. Then in onReceive in broadcast receiver i call activity that is enabling user t

3条回答
  •  一生所求
    2020-12-15 00:05

    I started off much like you, and the window flags didn't really work.

    I finally got it to work by using two Android services: KEYGUARD_SERVICE and POWER_SERVICE:

    KeyguardManager km = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
    final KeyguardManager.KeyguardLock kl = km.newKeyguardLock("MyKeyguardLock");
    kl.disableKeyguard();
    
    PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
    wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP
            | PowerManager.ON_AFTER_RELEASE, "MyWakeLock");
    wakeLock.acquire();
    

    Don't forget to release the wake lock when you're done with it.

    You'll need to request permissions DISABLE_KEYGUARD and WAKE_LOCK

提交回复
热议问题