BroadcastReceiver's onReceive() is failing to receive the broadcast sent by another BroadcastReceiver

738 views Asked by At

I have a Service from where I send a broadcast intent to a BroadcastReceiver and then this broadcast receiver sends it back to a BroadcastReceiver specified inside the Service.

Here's the code:

private Notification getNotificationAU() {
        mBuilder =
                new NotificationCompat.Builder(getBaseContext())
                        .setSmallIcon(R.mipmap.app_icon_1)
                        .setContentTitle("title")
                        .setContentText(***);

        // for action button
        Intent actionIntent = new Intent(getBaseContext(), BroadcastSender.class);
        PendingIntent actionPendingIntent = PendingIntent
                .getBroadcast(getBaseContext(),
                        0, actionIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        mBuilder.setAutoCancel(true);
        mBuilder.addAction(***, "***", actionPendingIntent);
        return mBuilder.build();
    }

Here's BroadcastSender.class code:

public class BroadcastSender extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {

        Toast.makeText(context, "Broadcast Received by BroadcastSender", Toast.LENGTH_SHORT).show();

        // send back to your class
        Intent newIntent = new Intent();
        newIntent.setAction(context.getString(R.string.broadcast_id));
        context.sendBroadcast(newIntent);
        context.sendBroadcast(new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS));
        Toast.makeText(context, "Broadcast sent back.", Toast.LENGTH_SHORT).show();

    }
}

both the Toasts above is getting shown.

Here's BroadcastReceiver inside the Service:

    public class MyBroadcastReceiver extends BroadcastReceiver {

        public MyBroadcastReceiver(){
            super();
        }

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

            Toast.makeText(context, "onReceive() in service called", Toast.LENGTH_SHORT).show();

        if (intent.getAction() != null && intent.getAction().equals(getString(R.string.broadcast_id_2))) {
               ...
               ...
               ...
        } else if (intent.getAction() != null && intent.getAction().equals(getString(R.string.broadcast_id))) {

                // perform the task

        } else {
                Toast.makeText(context, "Intent is null.", Toast.LENGTH_SHORT).show();
        }
        }
    }

Here's the strings.xml:

<string name="broadcast_id">***</string>

Here's AndroidManifest.xml:

<receiver android:name=".BroadcastSender"/>

The problem is that MyBroadcastReceiver is not receiving any broadcast even when both have same broadcast id R.string.broadcast_id.

Why it isn't receiving the broadcast sent by BroadcastSender.class?

Please let me know.

1

There are 1 answers

0
Hammad Nasir On BEST ANSWER

The problem here was not adding the line myIntentFilter.addAction(getString(R.string.broadcast_id)); while registering the BroadcastReceiver.

It got solved after adding the line.

Thanks to Jitesh Mohite and Hasif Seyd for helping me finding out this small but crucial mistake.