简体   繁体   English

Android BroadcastReceiver安装后无法正常工作

[英]Android BroadcastReceiver not working after install

Greetings! 问候! I'm working on an application that has a BroadcastReceiver listening on "android.intent.action.PHONE_STATE", which is run when the phone state is changed (I used it to run code when an incoming call is detected). 我正在研究一个应用程序,它有一个侦听“android.intent.action.PHONE_STATE”的BroadcastReceiver,它在手机状态改变时运行(我在检测到来电时用它来运行代码)。 This works fine on most of my test phones, but a few don't seem to trigger the receiver at all. 这在我的大多数测试手机上运行良好,但有些似乎根本没有触发接收器。 However, once these problem phones are restarted everything works as expected. 但是,一旦这些问题手机重新启动,一切都按预期工作。

Is there a reason these phones need to be restarted before the BroadcaseReceiver can pick anything up? 在BroadcaseReceiver可以选择任何东西之前,是否需要重新启动这些手机? Is there a way I can detect if it isn't running and manually "start" the BroadcaseReceiver? 有没有办法可以检测它是否没有运行并手动“启动”BroadcaseReceiver? I'm stumped, so I'd appreciate any help you can offer. 我很难过,所以我很感激你能提供的任何帮助。

Thank You. 谢谢。

To expand on the issue: starting from Android 3.1, installed applications are put in "STOPPED" state. 要扩展此问题:从Android 3.1开始,已安装的应用程序将处于“已停止”状态。 To invoke BroadcastReceiver(s) from stopped application an additional broadcast intent flag is required. 要从已停止的应用程序调用BroadcastReceiver,需要额外的广播意图标志

More details: http://developer.android.com/sdk/android-3.1.html#launchcontrols 更多细节: http//developer.android.com/sdk/android-3.1.html#launchcontrols

I created FLAG_INCLUDE_STOPPED_PACKAGES constant (=32) in my application (for pre-Android 3.1) and just add it to my broadcast intent intent.addFlags(FLAG_INCLUDE_STOPPED_PACKAGES); 我在我的应用程序中创建了FLAG_INCLUDE_STOPPED_PACKAGES常量(= 32)(对于Android 3.1之前的版本)并将其添加到我的广播意图intent.addFlags(FLAG_INCLUDE_STOPPED_PACKAGES);

Is there a reason these phones need to be restarted before the BroadcaseReceiver can pick anything up? 在BroadcaseReceiver可以选择任何东西之前,是否需要重新启动这些手机?

Assuming that your application has its BroadcastReceiver registered in the manifest for the PHONE_STATE broadcast, it should work immediately upon install. 假设您的应用程序在PHONE_STATE广播的清单中注册了BroadcastReceiver ,它应该在安装后立即生效。 If it does not, it feels like a buggy ROM to me. 如果没有,那对我来说感觉就像一辆车。

Is there a way I can detect if it isn't running and manually "start" the BroadcaseReceiver? 有没有办法可以检测它是否没有运行并手动“启动”BroadcaseReceiver?

No, mostly because it's not running, usually, even when things are working. 不,主要是因为它通常没有运行,即使事情正常。 An instance of your BroadcastReceiver is created at the point of the Intent - <intent-filter> match, it is called with onReceive() , and the BroadcastReceiver is disposed of once onReceive() returns. Intent - <intent-filter>匹配点创建BroadcastReceiver的实例,使用onReceive()调用它,并在onReceive()返回时处理BroadcastReceiver

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM