android监听屏幕打开关闭广播无响应的情况

2023-05-19,,

android在屏幕打开和关闭的时候会发出广播,但是如果receiver配置在AndroidManifest.xml中时,receiver是接受不到任何广播的。

<receiver android:name="cn.abcd.listener.ScreenStateReceiver" >
<intent-filter>
<action android:name="android.intent.action.SCREEN_OFF" />
<action android:name="android.intent.action.SCREEN_ON" />
</intent-filter>
</receiver>

只能在代码中用context 去regist 这个receiver

BroadcastReceiver receiver = new ScreenStateReceiver();
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_SCREEN_OFF);
filter.addAction(Intent.ACTION_SCREEN_ON);
App.context.registerReceiver(receiver, filter);

略坑的是,android developer上面居然没有直接说明。

简单查了下原因,大概是源码中发送这两个广播时设置了Intent.FLAG_RECEIVER_REGISTERED_ONLY这个flag。

android监听屏幕打开关闭广播无响应的情况的相关教程结束。

《android监听屏幕打开关闭广播无响应的情况.doc》

下载本文的Word格式文档,以方便收藏与打印。