Android notification of screen off/on

前端 未结 3 388
别那么骄傲
别那么骄傲 2020-12-10 02:17

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/di

3条回答
  •  -上瘾入骨i
    2020-12-10 02:40

    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);
    

提交回复
热议问题