Turn off the screen using the proximity sensor

I have a problem using the proximity sensor to turn off the screen. When My Device uses SDK version 21 or higher, I can use this code to turn off the screen when the user puts the phone next to something. (Capture SensorEventListener events)

public void turnOnScreen(){
    // turn on screen
    Log.v("ProximityActivity", "ON!");
    mWakeLock = mPowerManager.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "tag");
    mWakeLock.acquire();
}

@TargetApi(21) //Suppress lint error for PROXIMITY_SCREEN_OFF_WAKE_LOCK
public void turnOffScreen(){
    // turn off screen
    Log.v("ProximityActivity", "OFF!");
    mWakeLock = mPowerManager.newWakeLock(PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK, "tag");
    mWakeLock.acquire();
}

Now the problem is that I don’t know how to get the same behavior before API 21. I would like to put the screen in black and turn off all events on it.

Thanks in advance.

+4
source share

Source: https://habr.com/ru/post/1672187/


All Articles