domingo, 14 de octubre de 2012

How to detect if the phone screen is ON?


Problem:

We need to check if the screen of the phone is on or not.

Solution:

To implement that you have to follow this example. Please take note of the "isScreenOn" boolean variable.

@Override
protected void onPause()
{
    super.onPause();

    // If the screen is off then the device has been locked
    PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
    boolean isScreenOn = powerManager.isScreenOn();

    if (!isScreenOn) {

        // The screen has been locked 
        // do stuff...
    }
}

No hay comentarios:

Publicar un comentario