Listening for ACTION_SCREEN_OFF

前端 未结 2 1777
执笔经年
执笔经年 2020-11-27 19:19

I\'m trying to start a service that runs in the background that is listening for ACTION_SCREEN_OFF and when it finds ACTION_SCREEN_OFF, starts my a

相关标签:
2条回答
  • 2020-11-27 19:35

    I am using callback from ScreenReceiver to resolve problem onResume() and onPause(). These are called before the BroadcastReceiver.onReceive().

    0 讨论(0)
  • 2020-11-27 19:43

    You cannot declare ACTION_SCREEN_ON and ACTION_SCREEN_OFF in the AndroidManifest.xml. You are only allowed to catch them while your activity is running.

    Here's an example.

    The BroadcastReceiver:

    public class ScreenReceiver extends BroadcastReceiver {
    
        public static boolean wasScreenOn = true;
    
        @Override
        public void onReceive(final Context context, final Intent intent) {
            if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
                // do whatever you need to do here
                wasScreenOn = false;
            } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
                // and do whatever you need to do here
                wasScreenOn = true;
            }
        }
    
    }
    

    The Activity:

    public class ExampleActivity extends Activity {
    
        private BroadcastReceiver mReceiver = null;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            // initialize receiver
            final IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
            filter.addAction(Intent.ACTION_SCREEN_OFF);
            mReceiver = new ScreenReceiver();
            registerReceiver(mReceiver, filter);
            // your code
        }
    
        @Override
        protected void onPause() {
            // when the screen is about to turn off
            if (ScreenReceiver.wasScreenOn) {
                // this is the case when onPause() is called by the system due to a screen state change
                Log.e("MYAPP", "SCREEN TURNED OFF");
            } else {
                // this is when onPause() is called when the screen state has not changed
            }
            super.onPause();
        }
    
        @Override
        protected void onResume() {
            super.onResume();
            // only when screen turns on
            if (!ScreenReceiver.wasScreenOn) {
                // this is when onResume() is called due to a screen state change
                Log.e("MYAPP", "SCREEN TURNED ON");
            } else {
                // this is when onResume() is called when the screen state has not changed
            }
        }
    
        @Override
        protected void onDestroy() {
            if (mReceiver != null) {
                unregisterReceiver(mReceiver);
                mReceiver = null;
            }
            super.onDestroy();
        }
    
    }
    

    You could probably solve your question by listening to these events from a Service.

    0 讨论(0)
提交回复
热议问题