2

When user press a button, I would like to set a timer and turn off the screen immediately.

Had searched but looks like it requires DEVICE_POWER permission, which only available for system app.

Is it possible for a normal app to do this?

Deqing
  • 12,998
  • 13
  • 78
  • 120

1 Answers1

1

Try

<uses-permission android:name="android.permission.WAKE_LOCK" />

and

PowerManager.WakeLock acquire()

So something like

int field = PowerManager.class.getClass().getField("PROXIMITY_SCREEN_OFF_WAKE_LOCK").getInt(null);
PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
PowerManager.WakeLock wakeLock = powerManager.newWakeLock(field, getLocalClassName());

wakeLock.acquire();

Check out acquire and Turn off screen programmatically when face is close the screen on Android

Community
  • 1
  • 1
AmmarCSE
  • 29,061
  • 5
  • 39
  • 52