监听屏幕SCREEN_ON和SCREEN_OFF这两个action,实现屏幕锁定状态的监听,从而实现自己的锁屏效果,奇怪的是,这两个action只能通过代码的形式注册,才能被监听到,使用AndroidManifest.xml 完全监听不到。查了一下,发现这是PowerManager那边在发这个广播的时候,做了限制,限制只能有register到代码中的receiver才能接收。
- private void registerScreenActionReceiver(){
- final IntentFilter filter = new IntentFilter();
- filter.addAction(Intent.ACTION_SCREEN_OFF);
- filter.addAction(Intent.ACTION_SCREEN_ON);
- registerReceiver(receiver, filter);
- }
- private final BroadcastReceiver receiver = new BroadcastReceiver(){
- @Override
- public void onReceive(final Context context, final Intent intent) {
- // Do your action here
- }
- };