Android screen off

I cannot turn off the screen using this code. I used the method PowerManagerand wl.release(), but it does not work. Can someone 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 to the manifest, but the result was not.

+5
source share
5 answers

I found the answer to 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 and it seems to work.

+2
source

, SecurityException , . : ( WakeLock , , (onPause)

//declared globally so both functions can access this
public PowerManager.WakeLock wl;

///////////onCreate
//stop phone from sleeping
PowerManager powman = (PowerManager) getSystemService(Context.POWER_SERVICE);
wl = powman.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK, "NameOfLock");
wl.acquire();

///////////onPause
wl.release();

//////////for completion sake, onResume
if(!wl.isHeld()){
    wl.acquire();
}

if(stateString=="nextone")

if(stateString.equals("nextone"))

0

, , . , .

0

getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
0
try 
{
    Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_OFF_TIMEOUT, 1000*15);
} 
catch (NumberFormatException e) 
{
    Log.e("aa", "could not persist screen timeout setting", e);
}

0

All Articles