Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Turn screen Off

Tags:

android

screen

I can't turn off the screen using this code. I used PowerManager and wl.release() method, but it doesn't work. Can somebody show me an example?

  PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); 
   wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, "DoNotDimScreen"); 

This is part of my function:

  stateString = "nextone";
  if(stateString=="nextone"){        
  wl.release();
   }

I also added permission in the manifest but no result.

like image 440
AndBegginer Avatar asked Aug 25 '11 23:08

AndBegginer


1 Answers

I found the answer over here on stack overflow: Turn off screen on Android

Copied from there:

WindowManager.LayoutParams params = getWindow().getAttributes(); 
params.flags |= LayoutParams.FLAG_KEEP_SCREEN_ON; 
params.screenBrightness = 0; 
getWindow().setAttributes(params);

I tried this out and it seems to work.

like image 75
Gdalya Avatar answered Nov 10 '22 00:11

Gdalya