Reputation: 5256
Got an issue with my BroadcastReceiver
not ever being fired. Here's the definition of the receiver:
public class UIUpdater extends BroadcastReceiver {
private final Handler handler;
public UIUpdater(Handler handler)
{
this.handler = handler;
}
@Override
public void onReceive(final Context context, final Intent intent)
{
String action = intent.getAction();
Log.i("MAIN", " >>> ACTION <<< : " + action);
if(action.equalsIgnoreCase("PROGRESSINIT"))
{
int size = intent.getIntExtra("PROGRESSSIZE", 0);
updatePopulateProgressSize(size);
}
handler.post(new Runnable(){
@Override
public void run(){
updatePopulateProgress(intent.getIntExtra("PROGRESS", 0));
}
});
}
}
Here's the Manifest:
<receiver android:name="com.goosesys.gaggle.Main$UIUpdater">
<intent-filter>
<action android:name="com.goosesys.gaggle.populate"/>
</intent-filter>
</receiver>
And here's where I attempt to use it:
JSONArray array = new JSONArray(xmppBack.getPayload());
Intent intent = new Intent(Constants.BC_POPULATE);
intent.setAction(Constants.BC_PROGRESSINIT);
intent.putExtra(Constants.BC_PROGRESSSIZE, array.length());
GaggleApplication.getContext().sendBroadcast(intent);
Oh, and the constants:
public static final String BC_POPULATE = "com.goosesys.gaggle.populate";
public static final String BC_PROGRESSINIT = "PROGRESSINIT";
public static final String BC_PROGRESSSIZE = "PROGRESSSIZE";
public static final String BC_PROGRESSINC = "PROGRESSINC";
Thing is, I've stepped through each and every part and all is fine, no errors, no leaks, no problems. Just that the onReceive
is never popped :(
I'm new to broadcastreceivers, please be gentle ;)
Cheers in advance!
Upvotes: 0
Views: 4650
Reputation: 104
You need to register your receiver somewhere. Usually in onCreate method, i do it like :
registerReceiver(new UIUpdater(mhandler), new IntentFilter());
Upvotes: 0
Reputation: 1006539
Had you looked at LogCat, you would have found a warning or error, with a stack trace complaining about the fact that Android cannot create an instance of UIUpdater
. That is for two reasons:
It is not a static
inner class, and so the framework cannot create an instance of it (instances can only be created by the outer Main
class instance)
It does not have a zero-argument constructor, and that's what the framework will use
Also, this is not a good use case for a regular system broadcast like this. Please use an in-process event bus (LocalBroadcastManager
, Square's Otto, greenrobot's EventBus, etc.). That will improve performance (no IPC), improve security (right now, anyone can hack your receiver), and may simplify your code (if you use Otto or EventBus).
Upvotes: 3