Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android detect phone lock event

Tags:

I want to be able to detect the phone lock event. When my app is running, if I press the red button (call end button/power button), the phone gets locked and the screen goes blank. I want to be able to detect this event, is it possible?

like image 555
Chris Avatar asked Jul 03 '10 06:07

Chris


1 Answers

Alternatively you could do this:

@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;     if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH) {         isScreenOn = powerManager.isInteractive();     } else {         isScreenOn = powerManager.isScreenOn();     }      if (!isScreenOn) {          // The screen has been locked          // do stuff...     } } 
like image 139
Robert Avatar answered Oct 13 '22 20:10

Robert