Search code examples
androidbroadcastreceiver

Android: Broadcast not received when Action is specified


I've got a problem I'm researching for a few hours now.

I simply want to send a broadcast from one activity to another. I send the broadcast from activity A by:

Intent i = new Intent("test");
i.setAction("testaction");
sendBroadcast(i);

and receive it in Activity B by:

private BroadcastReceiver updateGUIReceiver = new BroadcastReceiver() {
@Override
    public void onReceive(Context context, Intent intent) {
        Log.e("test", "test")
        intent.getAction().equals(("testaction")){
        ...
        }
    }
}

....

registerBroadcastreceiver(updateGUIReceiver);

But the receiver never gets called if i specify the action. If I don't (uncomment setaction), the broadcast isn't being received, as well as attached extras and it works perfectly.


Solution

  • In your receiver Activity, change the line

    registerBroadcastreceiver(updateGUIReceiver);
    

    to

    IntentFilter filter = new IntentFilter();
    filter.addAction("testaction");
    registerReceiver(updateGUIReceiver, filter);
    

    Hope this help! :D