广播接收器和暂停活动
我在活动中以编程方式注册了一个广播接收器。它响应 PACKAGE_REMOVED
意图,该意图在包被删除时触发。
问题是,它没有收到此消息。我认为这是因为当我离开该活动并移动到另一个活动以卸载应用程序时会触发意图,因此原始活动已暂停。 是否暂停的活动(接收器未在 onPause
中取消注册)也会暂停接收器?
I have a broadcast receiver registered programatically in a activity. It responds to the PACKAGE_REMOVED
intent, that fires when a package gets removed.
The problem is, it doesn't get this message. I think this is due to that the intent is fired when I leave the activity and move to another activity to uninstall a app, so the original activity is paused.
Could it be that a paused activity (where the receiver is not unregistered in onPause
) also pauses the receiver?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当您在活动中以编程方式注册广播接收器时,当活动暂停时它将不会收到广播。 BroadcastReceiver docs 在这一点上并不那么清楚。他们建议在 onPause 上注销只是为了减少系统开销。
如果您希望即使 Activity 不在前台也能接收事件,请使用 接收器 元素。
When you register a broadcast receiver programatically in an activity, it will NOT get broadcasts when the activity is paused. The BroadcastReceiver docs are not as clear as they could be on this point. They recommend unregistering on onPause solely to reduce system overhead.
If you want to receive events even when your activity is not in the foreground, register the receiver in your manifest using the receiver element.
将 Receiver 添加到您的项目中,您甚至无需启动即可获得此事件您的申请。
并在您的清单中添加如下内容(在您的标签内):
当您使用这样的接收器时,您不会调用任何注册或取消注册,因此它将始终准备好获取数据。
请注意,如果您让用户将您的应用程序移至 SD 卡,则此操作将不起作用。如果在卸载 SD 卡时发送事件,则将无法访问接收器,并且您将错过该事件。
Add a Receiver to your project and you will get this event without even starting your application.
and in your manifest add it like this (Inside your <application> tag):
When you use a receiver like this you do not call any register or unregister so it will always be ready to get data.
A note is that this will not work if you let the users move your app to the SD card. If an event is sent when the SD card is unmounted the receiver will not be accessible and you will miss the event.
也许您可以在服务中注册接收器,该接收器将在后台运行
Maybe you can register the receiver in service which will run background