androidbroadcastreceiverandroid-broadcast

Sending another Broadcast inside BroadcastReceiver not working?


Is it possible to send a broadcast from one BroadcastReceiver to another BroadcastReceivers?

I am running first broadcast receiver perfectly with below call:

Intent intent = new Intent(CalendarAlarmManager.ACTION_CHECK_NEXT_ALARM);
intent.setClass(context, CalendarProviderBroadcastReceiver.class);
context.sendBroadcast(intent);

In first receiver, I'm doing some stuff and then running second broadcast receiver:

Intent intent = new Intent(ACTION_EVENT_REMINDER);
intent.setClass(context, AlarmReceiver.class);
context.sendBroadcast(intent);   // method is being called

Inside AndroidManifest:

        <receiver android:name=".CalendarProviderBroadcastReceiver"
            android:exported="true">
            <intent-filter>
                <action android:name="com.example.SOMETHING.TEST" />
            </intent-filter>
        </receiver>

        <receiver android:name=".AlarmReceiver"
            android:exported="false">
            <intent-filter>
                <action android:name="com.exmaple.SOMETHING.REMIND_ME" />
            </intent-filter>
        </receiver>

But surprisingly second receiver's onReceive method is not being called! Is ther any limitation in android for sending broadcast from another broadcast receiver?


Solution

  • The answer is yes. There is no limitation in sending another broadcast from a broadcast.

    I simply changed AndroidManifest from this:

            <receiver android:name=".AlarmReceiver"
                android:exported="false">  <--- This line is the problem
                <intent-filter>
                    <action android:name="com.exmaple.SOMETHING.REMIND_ME" />
                </intent-filter>
            </receiver>
    

    to this:

            <receiver android:name=".AlarmReceiver"
                android:exported="true">  <--- It is fixed now.
                <intent-filter>
                    <action android:name="com.exmaple.SOMETHING.REMIND_ME" />
                </intent-filter>
            </receiver>
    

    The problem with my code is that my broadcasts are defined inside a module other than App module. And maybe because of that, android is considering it as other application's receiver.