Coova
Coova

Reputation: 1858

Unable to receive broadcast from Service

The issue I am having is in regards to receiving a broadcast from a service that is within an android library project.

Why am I unable to receive the broadcast? does it have to do with all the service/interface logic being in a seperate android library project?

Here is the broadcast from the service

    broadcastReceivedMessage("THE TOPIC", "THE MESSAGE");

Here is the method within the service

    private void broadcastReceivedMessage(final String topic, final String message) {
/*      handler.post(new Runnable() {
            @Override
            public void run() {*/
                Intent broadcastIntent = new Intent(CALLBACK_ACTION);
                broadcastIntent.setAction(ON_MESSAGE_RECEIVED);
                broadcastIntent.putExtra(TOPIC_STR, topic);
                broadcastIntent.putExtra(MESSAGE_STR, message);
                sendBroadcast(broadcastIntent);
/*          }
        });*/

    }

Inside a class that implements many interfaces, this is where I am registering to listen for a specific intent filter.

  @Override
  public void connect(String address, int port) {
    IntentFilter filter = new IntentFilter(MQTTService2.CALLBACK_ACTION);
    LocalBroadcastManager.getInstance(getContext()).registerReceiver(serviceActionReceiver, filter);
    Bundle args = new Bundle();
    args.putString(MQTTService2.BROKER_ADDR_STR, address);
    args.putInt(MQTTService2.BROKER_PORT_INT, port);
    startService(MQTTService2.ACTION_CONNECT, args);
  }

Inside an Activity where I expect to receive a callback from the interfaces and get my broadcast

  MQTTEventHandler mHandler = new MQTTEventHandler() {

  @Override
  public void onStatusChanged(CommunicatorStatus status, String message) {
    // TODO Auto-generated method stub
    Log.e("onStatusChanged", "----------------Status Changed------------");
    Log.e("STATUS: ", status.toString());
    Log.e("MESSAGE: ", message);
    Log.e("onStatusChanged", "--------------------------------------------");

  }

  @Override
  public void onMessageReceived(String topic, String message) {
    // TODO Auto-generated method stub
    Log.e("onMessageReceived", "----------------Message Received------------");
    Log.e("TOPIC: ", topic);
    Log.e("MESSAGE: ", message);
    Log.e("onMessageReceived", "--------------------------------------------");

  }

  @Override
  public void onException(String message) {
    // TODO Auto-generated method stub

  }
};

mComm.addEventHandler(mHandler);

BroadcastReceiver

class ServiceActionReceiver extends BroadcastReceiver {



    @Override
    public void onReceive(Context context, Intent intent) {

      String action = intent.getAction();
      if (action.equals(MQTTService2.ON_EXCEPTION)) {
        Bundle extras = intent.getExtras();
        if (extras != null && extras.containsKey(MQTTService2.MESSAGE_STR))
          fireOnException(extras.getString(MQTTService2.MESSAGE_STR));
      } else if (action.equals(MQTTService2.ON_MESSAGE_RECEIVED)) {
        Bundle extras = intent.getExtras();
        if (extras != null && extras.containsKey(MQTTService2.MESSAGE_STR)
            && extras.containsKey(MQTTService2.TOPIC_STR))
          fireOnMessageReceived(extras.getString(MQTTService2.TOPIC_STR),
              extras.getString(MQTTService2.MESSAGE_STR));

      } else if (action.equals(MQTTService2.ON_STATUS_CHANGED)) {
        Bundle extras = intent.getExtras();
        if (extras.containsKey(MQTTService2.STATUS_CODE)) {
          MQTTService2.MQTTConnectionStatus status =
              MQTTConnectionStatus.parseCode(extras.getString(MQTTService2.STATUS_CODE));
          switch (status) {
            case CONNECTED:
              fireOnStatusChanged(CommunicatorStatus.DISCONNECTED,
                  extras.getString(MQTTService2.MESSAGE_STR, ""));
              break;
            case CONNECTING:
              break;
            case INITIAL:
              break;
            case NOTCONNECTED_DATADISABLED:
            case NOTCONNECTED_UNKNOWNREASON:
            case NOTCONNECTED_USERDISCONNECT:
            case NOTCONNECTED_WAITINGFORINTERNET:
              fireOnStatusChanged(CommunicatorStatus.DISCONNECTED,
                  extras.getString(MQTTService2.MESSAGE_STR, ""));
              break;
            default:
              break;

          }
        }

      }
    }
  }

Method called depending on specific intent filter ( onMessageReceived)

      private void fireOnMessageReceived(String topic, String message) {
    for (MQTTEventHandler eventHandler : handlers) {
      eventHandler.onMessageReceived(topic, message);
    }
/*    for (Iterator<MQTTEventHandler> it = handlers.iterator(); it.hasNext();) {
      it.next().onMessageReceived(topic, message);
    }*/
  }

Upvotes: 1

Views: 528

Answers (1)

Mike M.
Mike M.

Reputation: 39191

It seems the following is causing a mismatch between the Intent being broadcast from your Service, and the IntentFilter set on the BroadcastReceiver.

Intent broadcastIntent = new Intent(CALLBACK_ACTION);
broadcastIntent.setAction(ON_MESSAGE_RECEIVED);

The IntentFilter is set for the CALLBACK_ACTION, and the Intent is being instantiated with that same action, but the second line above is resetting the Intent's action to ON_MESSAGE_RECEIVED, causing it to no longer match the Filter on the BroadcastReceiver.

One possible solution would be to add multiple actions to the IntentFilter, and check the delivered action in the Receiver's onReceive() method to determine how to handle the broadcast.

For example:

IntentFilter filter = new IntentFilter(MQTTService2.ACTION_MESSAGE_RECEIVED);
filter.addAction(MQTTService2.ACTION_OTHER);

Then, in the onReceive() method:

@Override
public void onReceive(Context context, Intent intent)
{
    String action = intent.getAction();

    if(action.equals(ACTION_MESSAGE_RECEIVED)) {
        ...
    }
    else if(action.equals(ACTION_OTHER)) {
        ...
    }
    ...
}

You would then instantiate the broadcast Intent with the desired action, and remove the setAction() call afterward:

Intent broadcastIntent = new Intent(MQTTService2.ACTION_MESSAGE_RECEIVED);
...

Upvotes: 1

Related Questions