6

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.

Aniket Kapse
  • 315
  • 3
  • 20
AndBegginer
  • 233
  • 2
  • 5
  • 9
  • 1
    Well, your comparison with stateString will never be true. To compare strings in java you should use `stateString.equals("nextone")` instead. – Eloff Sep 01 '11 at 07:09

5 Answers5

2

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.

Community
  • 1
  • 1
Gdalya
  • 385
  • 3
  • 17
  • this solve the problem: getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); – ademar111190 Jul 02 '12 at 17:52
  • this code works fine to turn off screen,but how to turn it on back ? – Uniruddh Jun 10 '14 at 13:10
  • Setting params.screenBrightness to a value between 0 and 1 gives you brightness between 0% and 100%. Setting it to a negative value gives you auto brightness. I forget where I 1st saw this, but I use it all the time. – Gdalya Jun 11 '14 at 13:39
0

You can use

getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
skynet
  • 9,898
  • 5
  • 43
  • 52
ademar111190
  • 14,215
  • 14
  • 85
  • 114
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);
}

How to detect switching between user and device

Community
  • 1
  • 1
pengwang
  • 19,536
  • 34
  • 119
  • 168
0

If you don't use a permission, the program will crash with a SecurityException when it tries to lock, so that isn't the problem. The correct method is: (obtains WakeLock on start, gives it up when the application loses focus (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's sake, onResume
if(!wl.isHeld()){
    wl.acquire();
}

However, your problem is actually in this check

if(stateString=="nextone")

This should be if(stateString.equals("nextone"))

Dororo
  • 3,420
  • 2
  • 30
  • 46
0

please check this link before proceeding with wake lock. if it does not solve your problem then you can proceed with wake lock.

Force Screen On

Community
  • 1
  • 1
Samuel
  • 9,883
  • 5
  • 45
  • 57