获取Broadcast Receiver的onReceive触发代码
Get the code that trigger onReceive of Broadcast Receiver
class Receiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
doSth();
}
}
主要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);
在函数 doSth() 中,我想知道这个广播是由 intentA 还是 intentB 触发的,如果不明确地将其他信息放入 intent,我该如何做到这一点?在此先感谢。
你不能。因为 Intent
完全相同,因此 PendingIntent
也完全相同。您需要在 Intent
中添加一些内容以区分:Intent
ACTION、Intent
DATA 或一些 "extras".
class Receiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
doSth();
}
}
主要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);
在函数 doSth() 中,我想知道这个广播是由 intentA 还是 intentB 触发的,如果不明确地将其他信息放入 intent,我该如何做到这一点?在此先感谢。
你不能。因为 Intent
完全相同,因此 PendingIntent
也完全相同。您需要在 Intent
中添加一些内容以区分:Intent
ACTION、Intent
DATA 或一些 "extras".