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?
            Asked
            
        
        
            Active
            
        
            Viewed 3.2k times
        
    4 Answers
37
            
            
        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...
    }
}
 
    
    
        Volodymyr Yatsykiv
        
- 3,181
- 1
- 24
- 28
 
    
    
        Robert
        
- 37,670
- 37
- 171
- 213
- 
                    Do you need to add a permission to the application manifest for this one? – pqn Jul 08 '12 at 03:52
- 
                    1Tried this, and no need to add permission – HVNSweeting Jul 09 '12 at 01:52
- 
                    2I think this only check whether screen-on or not, not check the lock. This can mix with FLAG_KEEP_SCREEN_ON, so the screen-off only when you lock screen. http://developer.android.com/reference/android/os/PowerManager.html#isScreenOn() what about check unlocked ? (not screen-on) – HVNSweeting Jul 09 '12 at 01:57
- 
                    the answer here http://stackoverflow.com/questions/3462843/android-what-happens-when-device-is-unlocked – HVNSweeting Jul 09 '12 at 02:03
- 
                    2User isInteractive () instead of isScreenOn() for API 20 onwards – Najeebullah Shah Jan 12 '15 at 19:07
- 
                    How would I check if the device is locked, when in live wallpaper? – android developer Feb 25 '20 at 09:25
18
            Have a Broadcast Receiver
android.intent.action.SCREEN_ON
and
android.intent.action.SCREEN_OFF
Related: Read CommonsWare's Answer Here.
- 
                    19For the record -- lock off/on is not the same event as screen off/on. – greenoldman Oct 02 '12 at 17:00
- 
                    2
- 
                    Here's an example of this not catching the lock screen: on Android 5, if I switch users, the OS goes to the lock screen without turning the screen off. – Sam Feb 22 '15 at 04:18
- 
                    
- 
                    Another issue with this is: if the screen automatically turns of after the phone is left idle, the phone might not lock until 5 seconds afterwards. The broadcast is sent on screen off rather than on phone lock. – Sam Sep 30 '17 at 23:07
- 
                    Locking can occur without screen_off event, for example, if device is locked via device admin. – artem Oct 25 '17 at 16:44
0
            
            
        Register a broadcast with IntentFilter filter.addAction(Intent.ACTION_USER_PRESENT)
works pretty well even screen is turned on/off
 
    
    
        Khalid Lakhani
        
- 179
- 2
- 10
- 
                    No. It is not reliable. Does not get fired on some devices. – Michał Dobi Dobrzański Jan 24 '23 at 14:07
0
            
            
        Koltin format of Robert's solution.
 override fun onPause() {
        super.onPause()
        // If the screen is off then the device has been locked
        val powerManager = getSystemService(POWER_SERVICE) as PowerManager
        val isScreenOn: Boolean = powerManager.isInteractive
        
        if (!isScreenOn) {
            // The screen has been locked 
            // do stuff...
        }
    }
I am assuming Kitkat version is quite old already.
 
    
    
        Top4o
        
- 547
- 6
- 19
 
     
     
    