【问题标题】:updating alarm from pending intent in android从android中的待处理意图更新警报
【发布时间】:2010-12-27 12:53:08
【问题描述】:

我正在处理一个闹钟项目,我想编辑我已经设置的闹钟。 当我编辑警报时,警报时间会更新,但我使用 putExtra() 发送的值没有改变。我正在使用PendingIntent.FLAG_ONE_SHOT 标志。

但是当我设置标志PendingIntent.FLAG_UPDATE_CURRENT 时,所有putExtra() 值也会发生变化,但现在的问题是,当我点击停止按钮和finish() 时,它会再次调用当前活动。

意味着当我完成当前活动时,它会在按钮单击时再次调用它。 请帮我。 提前致谢。

【问题讨论】:

    标签: android android-alarms


    【解决方案1】:

    我在 AlarmManager 中更新 PendingIntent 的首选方法是取消它并重新设置它
    不要忘记取消:

    1) AlarmManager.cancel(pendingIntent) 具有与您的待处理意图匹配的 pendingIntent(相同的类,相同的操作......但不关心额外的,请参阅 IntentFilter
    2) pendingIntent.cancel();
    3) pendingIntent = new PendingIntent() ... 并进行其他设置
    4) AlarmManager.set(... 提供新的 PendingIntent

    【讨论】:

    • 在日历事件设置闹钟的情况下,如果我们使用局部变量而不是静态变量,我们可以为各种事件分配不同的时间。但是您似乎已经展示了静态的 pendingIntent 示例
    • 这真的是更新警报的唯一方法吗?为我工作。
    【解决方案2】:

    每个警报都有其唯一标识符,如果要更新警报,可以创建一个具有相同 UNIQUE_ID 的新警报。

    PendingIntent pi = PendingIntent.getBroadcast(this, PENDING_INTENT_ID, intent, 0);
    

    Check this answer

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-11-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多