Çağrı Aldemir
Çağrı Aldemir

Reputation: 95

Android Screen Turn On Programmatically

I'm using this codes for turn on the screen.

getWindow().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);

But after first using this codes, the screen never turn off again. I want to do turn on the screen but after that the screen again turn off when end of screen off time. How can I do it?

Upvotes: 1

Views: 2055

Answers (1)

Amir Uval
Amir Uval

Reputation: 14873

Turning the screen on:

getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

To turn off, you should clear the flag that you have set:

getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

Note that an application cannot force the screen to turn off, it can just release locks that it holds, so the system could turn the screen off as it would without your app.

Upvotes: 1

Related Questions