避免在Android中注册重复的广播接收器 [英] Avoid registering duplicate broadcast receivers in Android

查看:1540
本文介绍了避免在Android中注册重复的广播接收器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建我的第一个Android应用程序.我希望主线程活动(在我的情况下:ActionBarActivity)从后台活动(在我的情况下:IntentService)接收事件的通知.我已经读过,使用广播应该是做到这一点的最佳方法.

I am trying to create my first Android app. I would like a main-thread Activity (in my case: an ActionBarActivity) to receive notification of an event from a background Activity (in my case: an IntentService). I've read that using broadcasts should be the best way to do this.

要注册一个广播接收器以收听从后台活动发送的广播,我正在主线程活动中使用以下代码:

To register a Broadcast Receiver for listening to broadcasts sent from the background activity, I am using the following code inside the main-thread activity:

// Register broadcast receiver
LocalBroadcastManager bManager = LocalBroadcastManager.getInstance(this);
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("com.me.myBroadcast");
bManager.registerReceiver(bReceiver, intentFilter);

我尝试将其放入主线程活动的onCreate()方法中,但是我很快发现,每次重新启动活动(例如,关闭应用程序并重新打开它)时,似乎会创建重复"广播接收器,然后在每次发送单个广播时都会多次触发广播接收器的onReceive()方法.这会引起问题.

I tried putting this in the onCreate() method of my main-thread activity, but I quickly discovered that every time I restart the activity (e.g. closing the app and re-opening it), it seems to create "duplicate" broadcast receivers, which then trigger the Broadcast Receiver's onReceive() method multiple times whenever a single broadcast is sent. This is causing problems.

因此,我创建了一个SharedPreferences文件来保存一个布尔值,该布尔值会记住我是否已经创建了广播接收器,以避免创建重复项.完全按照希望的方式运行,直到我重新启动设备,然后广播接收器被销毁为止,并且该应用程序不会创建新的设备,因为SharedPreferences布尔值说它已经有一个.

So I created a SharedPreferences file to save a boolean which remembers whether or not I have already created a Broadcast Receiver, so as to avoid creating duplicates. This works exactly as hoped, until I restart the device of course, after which the Broadcast Receiver is destroyed, and the app doesn't create a new one because the SharedPreferences boolean says it already has one.

我猜我可以通过设置一个新的广播接收器来侦听设备重新启动来解决此问题,该设备重新启动会重置SharedPreferences布尔值,但是我有一种na的感觉,就是我使事情变得过于复杂.我想念什么?感谢您提供的任何帮助!

I'm guessing I could patch this issue by setting up a new broadcast receiver to listen for a device reboot, which resets the SharedPreferences boolean, but I have this nagging feeling that I am overcomplicating things massively. What am I missing? Thanks for any help offered!!

推荐答案

要回答您的问题,您可以忽略sharedprefs并在onResume中注册广播接收器,并在onPause中取消注册接收器.报告工作状态( 1 )android doc.请注意,这样,仅当活动"处于前台时,您才能获取状态更新.根据需要使用适当的生命周期方法.

To answer your question, you can ignore sharedprefs and register the broadcast receiver in onResume and unregister the receiver in onPause. This method is well described in Reporting Work Status(1) android doc. Note that, this way, you will get status updates only if Activity is in foreground. Use appropriate life cycle methods depending on your needs.

要将状态从服务报告回前景活动,我的偏好设置是ResultReceiver(

To report status back to foreground activity from service, my preference would have been ResultReceiver(2) class which feels natural compared to a broadcast. Also, if you need to report multiple status messages back, it will be clearer with statusCode param in onReceiveResult method of ResultReceiver class.

这篇关于避免在Android中注册重复的广播接收器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆