android.intent.action.SCREEN_ON 不能用作接收器 Intent 过滤器

标签 android broadcastreceiver

我正在尝试在屏幕打开时调用 BroadcastReceiver。在我的 AndroidManifest.xml 中,我指定了:

                <receiver android:name="IntentReceiver">
                    <intent-filter>
                            <action android:name="android.intent.action.SCREEN_ON"></action>
                    </intent-filter>
                </receiver>

然而,接收器似乎从未被调用(断点不触发,日志语句被忽略)。我已将 SCREEN_ON 换成 BOOT_COMPLETED 进行测试,这确实被调用了。

这是在 1.6(SDK 级别 4)项目中。

Google 代码搜索揭示了这一点,我下载了该项目并对其进行了同步,将其转换为使用最新工具,但它也无法拦截该事件。

http://www.google.com/codesearch/p?hl=en#_8L9bayv7qE/trunk/phxandroid-intent-query/AndroidManifest.xml&q=android.intent.action.SCREEN_ON

这可能不再受支持了吗?

以前我已经能够像这样调用 Context.registerReceiver() 成功拦截此事件

registerReceiver(new BroadcastReceiver() {

  @Override
  public void onReceive(Context context, Intent intent) {
    // ... 
  }
}, new IntentFilter(Intent.ACTION_SCREEN_ON));

然而,这是由长期存在的服务执行的。根据 CommonsWare 的明智建议,我选择尝试删除长期存在的服务并使用不同的技术。但我仍然需要检测屏幕关闭和打开事件。

最佳答案

Following sage advice from CommonsWare I have elected to try to remove the long-living Service and use different techniques.

实际上,我相信我的建议更像是浅蓝色...:-)

But I still need to detect the screen off and on events.

Android 不希望为某些事件启动新进程,因此设备不会因为所有必须同时运行的各种东西而变得太慢。 ACTION_SCREEN_ON 就是其中之一。看这个previous question有关该主题的淡蓝色建议。

因此,您需要问问自己,“我真的需要控制这些事件吗?”。如果您的回答是“否”,Android 核心团队会很高兴。

关于android.intent.action.SCREEN_ON 不能用作接收器 Intent 过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2575242/

相关文章:

android - Android 应用程序的根权限(完整的应用程序。不仅仅是几个命令)

android - 在广播接收器中保存数据

android - 改变 MediaRouteButton 的行为?

java - 在 Android Studio 上同步新项目时出现 Firebase 错误(ASCII 错误)

android - Google Play Location Service 的 BroadcastReceiver(状态栏的设置)

android - 判断Android Wear设备是否在线

android - BroadcastReceiver 向 IntentService 发送 Intent 时单元测试失败

java - Android - 其他应用窃取我的 Intent 接收器?

android - SlideDateTimePicker 日期时间选择器打开问题

Android Twitter 应用程序 - 无用