setRepeating不会从其他活动中取消

时间:2013-05-13 21:27:11

标签: android alarmmanager

我看到有很多类似的问题,但我的警报信息存储在数据库中。通过调用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);

     }

但它没有用....你能告诉我正确的代码让警报停止重复吗?

1 个答案:

答案 0 :(得分:1)

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