20

I'm looking to see if there's a system notification I can listen for to see when the screen turns off/on. Any thoughts? Something similar to when the network connects/disconnects.

Kevin Galligan
  • 16,159
  • 5
  • 42
  • 62

3 Answers3

27

Easiest way is to put this in your MyApplication.onCreate() method:

IntentFilter intentFilter = new IntentFilter(Intent.ACTION_SCREEN_ON);
intentFilter.addAction(Intent.ACTION_SCREEN_OFF);
registerReceiver(new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
            Log.d(TAG, Intent.ACTION_SCREEN_OFF);
        } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
            Log.d(TAG, Intent.ACTION_SCREEN_ON);
        }
    }
}, intentFilter);
Chris Lacy
  • 4,222
  • 3
  • 35
  • 33
  • How and when to unregister the Receiver in this case ? – Muhammad Riyaz Mar 17 '15 at 09:16
  • 1
    You can call [unregisterReceiver()](http://developer.android.com/reference/android/content/Context.html#unregisterReceiver(android.content.BroadcastReceiver)) at any point you like. But assuming you put the above code in `MyApplication.onCreate()`, you don't _have_ to do so because there's no Application.onDestroy() function (the `Application` class is unique that way - see the official docs or [here](http://stackoverflow.com/questions/17278201/android-ondestroy-or-similar-method-in-application-class) for more info). – Chris Lacy Mar 17 '15 at 20:34
12

The system will broadcast when the screen turns on and off -

In order to listen for these, you can create a BroadcastReceiver that listens for the events:

Intent.ACTION_SCREEN_OFF
Intent.ACTION_SCREEN_ON

They're listed in the documentation here:

Also, there's a tutorial about responding to these events that you might find useful.

Umair
  • 6,366
  • 15
  • 42
  • 50
Alexander Lucas
  • 22,171
  • 3
  • 46
  • 43
  • 6
    There's a difference between having the same answer and copying one. We did the same google search is all :P – Alexander Lucas Nov 22 '10 at 20:32
  • 1
    Read the doc carefully! This answer actually tells you if the device is "interactive". If the screen is locked, the device is not interactive. Only `android.hardware.display.DisplayManager` can tell you, if the display hardware is currently on. However, that probably requires Android 5. – OneWorld Sep 29 '16 at 11:02
1

For anyone looking for Kotlin equivalent code to the top answer , this worked for me:

val intentFilter = IntentFilter(Intent.ACTION_SCREEN_ON)
intentFilter.addAction(Intent.ACTION_SCREEN_OFF)
registerReceiver(object: BroadcastReceiver() {
    override fun onReceive(context:Context, intent:Intent) {
        if (intent.action == Intent.ACTION_SCREEN_OFF) {
            Log.d(TAG, Intent.ACTION_SCREEN_OFF)
        }
        else if (intent.action == Intent.ACTION_SCREEN_ON) {
             Log.d(TAG, Intent.ACTION_SCREEN_ON)
        }
    }
}, intentFilter)

(the auto Kotlin conversion in Android Studio didn't work for me, so I quickly rewrote the snippet - hopefully it saves someone else that extra minute or two)

dabarnard
  • 83
  • 1
  • 6