0

I want to detect the Chromebook lock, Unlock event. I am using Foreground service and register Broadcast to detect Intent.ACTION_SCREEN_ON, Intent.ACTION_SCREEN_OFF, Intent.ACTION_USER_PRESENT. Only getting Intent.ACTION_SCREEN_ON, Intent.ACTION_SCREEN_OFF broadcast but not getting Intent.ACTION_USER_PRESENT broadcast on Chromebook.

override fun onCreate() {
        super.onCreate()
        startForeground()

        intentFilter = IntentFilter(Intent.ACTION_SCREEN_ON)
        intentFilter = IntentFilter(Intent.ACTION_USER_PRESENT)
        intentFilter.addAction(Intent.ACTION_SCREEN_OFF)


       //other code

    }

class XYZBroadcastReceiver : BroadcastReceiver() {


    override fun onReceive(context: Context?, intent: Intent) {

        val action = intent.action

      
        if (Intent.ACTION_USER_PRESENT == action) {
        
        } 
        else if (Intent.ACTION_SCREEN_OFF == action) {
            
        }

        else if(Intent.ACTION_SCREEN_ON == action){
            
        }
    }

I am trying lots of methods from Android detect phone lock event, A way to get unlock event in android?, Android - detect phone unlock event, not screen on but nothing works for Chromebook. Is there any other way to detect lock, Unlock events on Android Chromebook?

Nikhil
  • 1,023
  • 17
  • 35

1 Answers1

0

The information you're referring to may be outdated ...

Since API level 26 you won't receive this implicit broadcast on regular Android either:

https://developer.android.com/guide/components/broadcast-exceptions

Martin Zeitler
  • 1
  • 19
  • 155
  • 216
  • I am not declared this in manifests, already explicitly registering Intent.ACTION_USER_PRESENT with Context.registerReceiver(). Its working fine in Google Pixel Android 11 but not working on Chromebook. – Nikhil Apr 12 '21 at 08:14
  • As the documentation reads: "Even though these implicit broadcasts still work in the background, you should avoid registering listeners for them." ...likely because there is no guarantee for the broadcast to happen. – Martin Zeitler Apr 12 '21 at 14:09