赞
踩
采用动态注册广播的方式对灭屏和亮屏进行监听
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);
参考:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。