Android 灭屏和亮屏的监听
采用动态注册广播的方式对灭屏和亮屏进行监听
1.创建receiver
class ScreenStatusReceiver extends BroadcastReceiver { String SCREEN_ON = "android.intent.action.SCREEN_ON"; String SCREEN_OFF = "android.intent.action.SCREEN_OFF"; @Override public void onReceive(Context context, Intent intent) { if (SCREEN_ON.equals(intent.getAction())) { //屏幕亮做xxx操作 } else if (SCREEN_OFF.equals(intent.getAction())) { //屏幕暗做xxx操作 } } }
2.使用receiver
private ScreenStatusReceiver mScreenStatusReceiver;
在onCreate
或onResume
中使用
private void registSreenStatusReceiver() { mScreenStatusReceiver = new ScreenStatusReceiver(); IntentFilter screenStatusIF = new IntentFilter(); screenStatusIF.addAction(Intent.ACTION_SCREEN_ON); screenStatusIF.addAction(Intent.ACTION_SCREEN_OFF); registerReceiver(mScreenStatusReceiver, screenStatusIF); }
在onDestroy
或onPause
中取消监听
unregisterReceiver(mScreenStatusReceiver);
参考:
android 灭屏监听,Android监听灭屏和亮屏
关于android广播监听亮灭屏的学习