获取触发onReceive of Broadcast Receiver的代码

时间:2019-11-04 08:21:10

标签: android android-intent broadcastreceiver android-pendingintent

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

在主要活动中:

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);

在函数doSth()中,我想知道是由intentA还是intentB触发了该广播,如何在没有明确将其他信息放入intent中的情况下执行此操作?

1 个答案:

答案 0 :(得分:1)

不能。由于Intent完全相同,因此PendingIntent也完全相同。您需要在Intent中添加一些内容以进行区分:Intent ACTION,Intent DATA或某些“附加”。