Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Screen on/off detection

Tags:

android

here i am trying to determine whether the screen is on or not but it doesn't seems to be working when press power lock/unlock button. Application works with no error but the code in if-else doesnt seems to have effect. Edited now the code work(thanks Olgun) but the mediaplayer playback doesnt stops and every time on/off screen new mediaplayer objct is created(multiple playbacks ).

SCBroadcaster.java

    public class SCBroadcaster extends BroadcastReceiver {
    PowerManager pm;
    MediaPlayer mp;
    public static boolean wasScreenOn = true;

    @Override
    public void onReceive(Context arg0, Intent arg1) {
        // TODO Auto-generated method stub
        mp = MediaPlayer.create(arg0, R.raw.gale);
        if (arg1.getAction().equals(Intent.ACTION_SCREEN_ON)) {
                    mp.stop();
            mp.release();
            Toast.makeText(arg0, "oNONONO", Toast.LENGTH_LONG).show();

        } else if (arg1.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
            mp.start();
        }

    }

}

MainActivity.java

public class MainActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_OFF);
    filter.addAction(Intent.ACTION_SCREEN_ON);
    BroadcastReceiver bre = new SCBroadcaster();
    registerReceiver(bre, filter);
}

protected void onPause() {
    super.onPause();
    if (SCBroadcaster.wasScreenOn) {
        System.out.println("SCREEN TURNED OFF");
    } else {

    }
}

protected void onResume() {
    super.onResume();
    if (!SCBroadcaster.wasScreenOn) {

    } else {

    }
}

}

like image 257
dreamer1989 Avatar asked Nov 09 '12 11:11

dreamer1989


People also ask

How do I turn off screen on Android?

Open Settings. Tap Display. Tap Sleep or Screen timeout. Select how long you want your Android smartphone or tablet screen to stay on before turning off due to inactivity.

How do I know if I have an Android screen?

IsScreenOn() returns a boolean that is true if the screen is turned on and false if the screen is turned off.


2 Answers

The approach with the ACTION_SCREEN did not work for me. After some different solutions this code finally solved the problem for me:

/**
 * Is the screen of the device on.
 * @param context the context
 * @return true when (at least one) screen is on
 */
public boolean isScreenOn(Context context) {
    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH) {
        DisplayManager dm = (DisplayManager) context.getSystemService(Context.DISPLAY_SERVICE);
        boolean screenOn = false;
        for (Display display : dm.getDisplays()) {
            if (display.getState() != Display.STATE_OFF) {
                screenOn = true;
            }
        }
        return screenOn;
    } else {
        PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
        //noinspection deprecation
        return pm.isScreenOn();
    }
}
like image 59
userM1433372 Avatar answered Oct 16 '22 05:10

userM1433372


Intent.ACTION_SCREEN_OFF and ACTION_SCREEN_ON check out for above broadcasts registration. here you can find a good example.

like image 28
Olgun Kaya Avatar answered Oct 16 '22 07:10

Olgun Kaya