2013-05-13 88 views
0

我看到有很多类似的问题,但我的警报信息存储在数据库中。通过调用deleteReminder,数据库内的提醒被取消,看起来像这样setRepeating不会从其他活动取消

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方法这样

公共无效setReminder(龙任务id,日历时,字符串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

不确定这是否是问题,但您正在使用'FLAG_ONE_SHOT'而不是'FLAG_UPDATE_CURRENT'来取消'PendingIntent'。你有没有试过做'FLAG_UPDATE_CURRENT'? – Karakuri 2013-05-13 21:31:05

+0

就是这样。谢谢! – JeffK 2013-05-13 22:09:22

+0

很酷。我会把它放在一个答案中,以便为其他人的利益做标记。 – Karakuri 2013-05-14 16:02:19

回答

1

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