9

I need to programmatically turn the screen on when the user turns off with power button, and yes I always have the correct flags in the Activity to keep screen on, but it does not avoid user pressing power button.

So far I've found a solution but it uses a deprecated wakelock

sWakeLock = sPM.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.ON_AFTER_RELEASE, TAG_WAKELOCK);

sWakeLock.acquire();
sWakeLock.release();

There is a better way to achieve it?

Vikasdeep Singh
  • 20,983
  • 15
  • 78
  • 104
sherpya
  • 4,890
  • 2
  • 34
  • 50
  • So the user has given a clear, unambiguous signal that they want the screen turned off, but you know better? This sounds like a dreadful user interface. –  May 07 '15 at 01:54
  • 1
    The screen should be always on, and I want to prevent accidental power button press in specific situations – sherpya May 07 '15 at 11:07

3 Answers3

18

In Lollipop you might want to add some more flags:

final Window win = getWindow();
    win.addFlags( WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
            WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD |
            WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON |
            WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON |
            WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON );

I got this from the AOSP https://android.googlesource.com/platform/packages/apps/DeskClock/+/dfd1960/src/com/android/deskclock/alarms/AlarmActivity.java

rubmz
  • 1,947
  • 5
  • 27
  • 49
11

Here is the latest way to Turn On Screen from an activity:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {
    this.setTurnScreenOn(true);
} else {
    final Window window = getWindow();
    window.addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
}

Use FLAG_TURN_SCREEN_ON but this flag has been deprecated in API level 27 so use Activity.setTurnScreenOn(true) from API level 27 onwards.

Vikasdeep Singh
  • 20,983
  • 15
  • 78
  • 104
  • 1
    Thank you. This information is very useful. – dcarl661 Mar 26 '21 at 16:43
  • 1
    this didnt work on Huawei but thank it worked until i addded more FLAGS like @rubmz mentioned WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON | WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON – Ridha Rezzag May 02 '21 at 14:17
11

Code

 PowerManager powerManager = (PowerManager) context.getSystemService(context.POWER_SERVICE);
    PowerManager.WakeLock  wakeLock = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK |
            PowerManager.ACQUIRE_CAUSES_WAKEUP |
            PowerManager.ON_AFTER_RELEASE, "appname::WakeLock");

    //acquire will turn on the display
    wakeLock.acquire();

    //release will release the lock from CPU, in case of that, screen will go back to sleep mode in defined time bt device settings
    wakeLock.release();

in the Manifest file, the permission should be

<uses-permission android:name="android.permission.WAKE_LOCK"/>
Mohamed Salman
  • 309
  • 2
  • 9