Search code examples
androidandroid-intentbroadcastreceiverandroid-pendingintent

Get the code that trigger onReceive of Broadcast Receiver


class Receiver extends BroadcastReceiver {
    @Override
        public void onReceive(Context context, Intent intent) {
            doSth();
        }
}

In the main activity:

Intent intentA = new Intent(MainApplication.getAppContext(), Receiver.class);
Intent intentB = new Intent(MainApplication.getAppContext(), Receiver.class);
PendingIntent pendingIntentA = PendingIntent
                         .getBroadcast(MainApplication.getAppContext(),0,intentA,0);
PendingIntent pendingIntentB = PendingIntent
                         .getBroadcast(MainApplication.getAppContext(),0,intentB,0);
alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + 5000,
                60000, pendingIntentA);
alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + 5000,
                60000, pendingIntentB);

In the function doSth() i want to know if this broadcast is triggered whether by intentA or intentB, how can i do that without explicitly putting additional information into the intent?, thanks in advance.


Solution

  • You can't. Because the Intents are exactly the same and because of that, the PendingIntents are also exactly the same. You will need to put something in the Intent to differentiate: either the Intent ACTION, Intent DATA, or some "extras".