我正在安排任务并根据日期和时间添加警报管理器,并且计划任务正在添加到列表中..当我添加任何任务时,它还在sqlite数据库中添加并为警报管理器分配pendinng意图的唯一ID。
现在,如果我想取消警报,那么如果我从列表中删除该行,那么我也想解除该特定警报..我能够从数据库中删除行但是如何解除该行的警报集? / p>
我的代码如下:
Button AddData=(Button)findViewById(R.id.senddata);
AddData.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v) {
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
int iUniqueId = (int) (System.currentTimeMillis() & 0xfffffff);
Date= updatedate.getText().toString();
Time= updateTime.getText().toString();
Discripton= discription.getText().toString();
//---get current date and time---
Calendar calendar = Calendar.getInstance();
//---sets the time for the alarm to trigger---
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.MONTH, month);
calendar.set(Calendar.DAY_OF_MONTH, day);
calendar.set(Calendar.HOUR_OF_DAY, mHour);
calendar.set(Calendar.MINUTE, mMinute);
calendar.set(Calendar.SECOND, 0);
Log.i("********####",year+" ,"+month+" , "+day+" , "+mHour+" , "+mMinute+"----"+ calendar.getTimeInMillis());
Intent intent = new Intent(AddEditExpense.this, TimeAlarm.class);
//intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
Bundle b12 = new Bundle();
mStuffresults=Discripton;
b12.putString("serverresponse", mStuffresults);
intent.setAction("" + Math.random());
intent.putExtras(b12);
PendingIntent displayIntent = PendingIntent.getBroadcast(AddEditExpense.this,iUniqueId,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.set(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), displayIntent);
}
}
请参阅此处:更新
public boolean onContextItemSelected(MenuItem item) {
switch(item.getItemId()) {
case DELETE_ID:
UniqueId=AddEditExpense.s;
Log.i("UniqueId",UniqueId);
Integer i = Integer.valueOf(UniqueId);
PendingIntent contentIntent = PendingIntent.getBroadcast(TaskReminder.this, i,
new Intent(),PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.cancel(contentIntent);
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
mDbHelper.deleteNote(info.id);
fillData();
return true;
}
答案 0 :(得分:12)
您可以使用以下命令检索为警报创建的PendingIntent:
PendingIntent displayIntent = PendingIntent.getBroadcast(AddEditExpense.this, iUniqueId, intent, PendingIntent.FLAG_NO_CREATE);
请务必使用您在sqlite数据库中保存的唯一ID来进行此警报。 现在你应取消AlarmManager中的PendingIntent并取消PendingIntent本身:
if(displayIntent != null) {
alarmManager.cancel(displayIntent);
displayIntent.cancel();
}
答案 1 :(得分:2)
问题在于,您没有提供与取消intent
时相同的pendingintent
,
在这里,您提到了要检索pendingintent
PendingIntent contentIntent = PendingIntent.getBroadcast(TaskReminder.this, i,
new Intent(),PendingIntent.FLAG_UPDATE_CURRENT);
我发现您在第一次创建new Intent()
时提供了intent
而不是pendingintent
。
如果您想要取消pendingintent
,那么您的意图应该是相同的,在您的情况下它将是
Intent intent = new Intent(AddEditExpense.this, TimeAlarm.class)
我相信您正在将唯一值i
传递给参数,该参数在创建和取消pendingintent
时也应具有相同的值。
欲了解更多信息, 请参阅我的回答HERE