【问题标题】:setRepeating won't cancel from other activitysetRepeating 不会从其他活动中取消
【发布时间】:2013-05-15 00:05:07
【问题描述】:

我看到有很多类似的问题,但我的警报信息存储在数据库中。通过调用 deleteReminder 在数据库中取消提醒,它看起来像这样

 public boolean deleteReminder(long rowId) {

    return mDb.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;


} 

这会删除提醒,但不会删除警报。我尝试将其更改为

public boolean deleteReminder(long rowId) {

    new ReminderManager(this).cancelReminder();


    return mDb.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;

}

并像这样在我的 ReminderManager 活动中添加了一个 cancelReminder 方法

public void setReminder(Long taskId, Calendar when, String spinInterval) { Log.e(TAG, spinInterval);

    long l = Long.parseLong(spinInterval);



    Intent i = new Intent(mContext, OnAlarmReceiver.class);
    i.putExtra(RemindersDbAdapter.KEY_ROWID, (long)taskId); 

    PendingIntent pi = PendingIntent.getBroadcast(mContext, 0, i, PendingIntent.FLAG_UPDATE_CURRENT); 

    mAlarmManager.setRepeating(AlarmManager.RTC_WAKEUP, when.getTimeInMillis(), l, pi);
}

public void cancelReminder(){

      Intent i = new Intent(mContext, OnAlarmReceiver.class);
      PendingIntent pi = PendingIntent.getBroadcast(mContext, 0, i, PendingIntent.FLAG_ONE_SHOT);

      mAlarmManager.cancel(pi);

     }

但它不起作用....您能告诉我正确的代码以使警报停止重复吗?

【问题讨论】:

  • 不确定这是否是问题所在,但您使用FLAG_ONE_SHOT 而不是FLAG_UPDATE_CURRENT 来取消PendingIntent。您是否尝试过同时制作FLAG_UPDATE_CURRENT
  • 酷。我会把它放在一个答案中,这样你就可以为了他人的利益而标记它。

标签: android alarmmanager


【解决方案1】:

使用PendingIntent.FLAG_UPDATE_CURRENT 调用PendingIntent.getBroadcast()。您发布的代码在您第二次调用时使用了PendingIntent.FLAG_ONE_SHOT

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-09-09
    • 2016-01-25
    • 2015-12-27
    • 1970-01-01
    • 2019-10-26
    • 2017-03-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多