【问题标题】:Get the code that trigger onReceive of Broadcast Receiver获取触发Broadcast Receiver的onReceive的代码
【发布时间】:2019-11-04 08:21:10
【问题描述】:
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 触发,如果不明确将其他信息放入意图中,我该如何做到这一点?提前谢谢。

【问题讨论】:

    标签: android android-intent broadcastreceiver android-pendingintent


    【解决方案1】:

    你不能。因为Intents 完全相同,因此PendingIntents 也完全相同。您需要在 Intent 中添加一些内容以区分:Intent ACTION、Intent DATA 或一些“附加”。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-03-12
      • 2023-01-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-10
      相关资源
      最近更新 更多