androidwakelockandroid-wake-lock

Turn off screen on Android


I am trying to turn on and off the display after a certain action happens (Lets just worry about turning the screen off for now). From what I understand from wake lock, this is what I have:

PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);    
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "My Tag");

When I read other posts on stackoverflow and else where, they seem to tell me that PARTIAL_WAKE_LOCK will turn the screen off. But if I read the SDK it says that it will only allow the screen to be turned off. I think this isn't right.


Solution

  • There are two choices for turning the screen off:

    PowerManager manager = (PowerManager) getSystemService(Context.POWER_SERVICE);
    
    // Choice 1
    manager.goToSleep(int amountOfTime);
    
    // Choice 2
    PowerManager.WakeLock wl = manager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Your Tag");
    wl.acquire();
    wl.release();
    

    You will probably need this permission too:

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

    UPDATE:

    Try this method; android turns off the screen once the light level is low enough.

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