Android getExtras/Intent与预期不同

Android getExtras/Intent与预期不同,android,android-intent,alarmmanager,Android,Android Intent,Alarmmanager,在我的活动中,我管理用户提供的食物列表,并根据它们的过期时间设置警报。然而,出于某种原因,传递给onRecieve的意图并不是我认为应该的 例如,用户输入过期日期为5/8的浆果和过期日期为6/4的黄油,然后点击保存数据。对于每个项,都会调用此方法。每次调用此方法时,我都会使用putExtra保存到期日期 public void setOneTimeAlarm(int daysAfterSet) { //declare intent using class that will ha

在我的活动中,我管理用户提供的食物列表,并根据它们的过期时间设置警报。然而,出于某种原因,传递给onRecieve的意图并不是我认为应该的

例如,用户输入过期日期为5/8的浆果和过期日期为6/4的黄油,然后点击保存数据。对于每个项,都会调用此方法。每次调用此方法时,我都会使用putExtra保存到期日期

    public void setOneTimeAlarm(int daysAfterSet) {
    //declare intent using class that will handle alarm
    Intent intent = new Intent(this, FoodExpAlarm.class);
    //retrieve pending intent for broadcast, flag one shot means will only set once
    intent.putExtra("expDate", expDate);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,
      intent, PendingIntent.FLAG_ONE_SHOT);
    //params: specify to use system clock use RTC_WAKEUP to wakeup phone for notification,
    //time to wait, intent
    alarmMan.set(AlarmManager.RTC_WAKEUP,
      System.currentTimeMillis() + (daysAfterSet * AlarmManager.INTERVAL_DAY), pendingIntent);
 }
这会导致为浆果设置一个警报,因为浆果即将过期,而黄油(离过期还有一个月)则没有警报。然后调用foodExpAlarm类一次,并正确显示浆果通知。然而,问题来自下一批食品何时进入。接下来,用户输入过期5/8的培根和过期5/15的香蕉。现在再次调用setOneTimeAlarm两次(每次调用一次),并且再次调用foodExpAlarm一次(培根)

然而,在foodExpAlarm中,传递进来的额外食物是黄油而不是培根。 这对我来说毫无意义,因为应该在时间过去之后调用foodExpAlarm,并且应该使用与计划时间对应的挂起意图来调用foodExpAlarm。然而,情况似乎并非如此,似乎意图(或至少是额外的)只与所有食物的添加顺序相对应

执行摘要:

  • 加入浆果5/8
  • 加入黄油6/4
  • 拯救
  • 正确显示浆果通知
  • 加培根5/8
  • 加入香蕉5/15
  • 拯救
  • 不正确地展示黄油
我的问题是,为什么我想要的是黄油而不是培根?我对意图的理解是错误的吗?我怎样才能解决这个问题

foodExpAlarm:

    public void onReceive(Context context, Intent intent) {

    Bundle exp = intent.getExtras(); //This gets the extras from butter, not bacon like I want
    Object temp = exp.get("expDate");  
    Enter_Foods.expDate = new DateTime(temp);
    intent.getExtras();
    int id = (int) (Enter_Foods.expDate).getMillis();
    ....

    notificationMan = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
    //i think this gets pending intent from the alarm that called this method
    PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
        new Intent(context,MainActivity.class), Intent.FLAG_ACTIVITY_NEW_TASK);
    //creates notification object/icon, and set text to flow across top bar
    Notification notif = new Notification(R.drawable.ic_launcher,
        "Food expiring soon", System.currentTimeMillis());
    //specify what to display when notification is shown
    notif.setLatestEventInfo(context, from, message, contentIntent);
    //use nofication manager to send message to phone, will update if same id
    notificationMan.notify(id, notif);
    }

我认为,由于报警管理器和意图的问题,您会遇到冲突。与报警管理器使用的意图比较方法相比,您的意图似乎是相同的,因为它们只是在“额外”方面有所不同,所以您的报警是相互残杀的

以下是对AlarmManager.set方法的引用:

安排闹钟。注:用于计时操作(滴答声、超时等) 使用Handler更简单、更高效。如果有 已为同一个IntentSender计划了一个警报,它将首先 取消了

如果时间发生在过去,则会触发报警 马上。如果已经有此目的的警报 (两个意图相等的定义如下: filterEquals(Intent)),则它将被删除并替换为 一个

然后在
filterEquals(Intent)

确定出于意图的目的,两个意图是否相同 分辨率(过滤)。也就是说,如果他们的动作、数据、类型、类, 和类别是一样的。这不会比较任何额外数据 包含在意图中


我认为这与Activity.setIntent()有关。我认为您必须重写onNewIntent并更新活动以使用新的意图..这会影响传递给OnReceive的参数吗?发布用于设置通知的代码。我更改了foodExpAlarm部分以显示通知代码这不会导致我的警报根本不触发吗?通知触发器和警报的调用方式与预期相同,但与通知一起传递的额外内容与警报调用的顺序不一致。例如,警报正确设置为1天3天2天5天,但不是按照时间顺序(1天2天3天5天)传递额外警报,而是按照警报输入的顺序传递额外警报,而不是在警报发出的时间。您是否发现随着时间的推移会发生多个警报?你最终会让所有4个警报发生吗?(我假设您可以更改android上的时间以实现这些目标)。在你的笔记中,我看到两次保存,其中两个条目只触发两个警报。是的,我最终会得到所有警报,我在帖子中提到的警报在食物输入后立即触发。其他警报直到6月1日和5月12日(截止日期前三天)才会发出。