I am scheduling task and adding alarm manager according to date and time and scheduled task is adding in a list ..when I add any task then it also added in sqlite database and assign a unique id for pendinng intent for alarm manager.
Now if I want to dismisss the alarm then if I remove the row from list then I want to dismiss that particular alarm also.. I am able to delete row from data base but how to dismiss alarm set for that row ?
My code is below :
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);
}
}
pls refer here also: UPDATED
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;
}
You can retrieve the PendingIntent that you created for the alarm with:
PendingIntent displayIntent = PendingIntent.getBroadcast(AddEditExpense.this, iUniqueId, intent, PendingIntent.FLAG_NO_CREATE);
Be sure to use the unique id that you saved in the sqlite database for this Alarm. Now you should cancel the PendingIntent in the AlarmManager and cancel the PendingIntent itself:
if(displayIntent != null) {
alarmManager.cancel(displayIntent);
displayIntent.cancel();
}
The problem is that, you did not provided the same intent
as when you cancel your pendingintent
,
here you mentioned your line of code to retrieve pendingintent
PendingIntent contentIntent = PendingIntent.getBroadcast(TaskReminder.this, i,
new Intent(),PendingIntent.FLAG_UPDATE_CURRENT);
I found that you are providing a new Intent()
instead of an intent
which you used while creating a pendingintent
at first time .
so intent should the be same when are you going to cancel your pendingintent
, in your case it will be
Intent intent = new Intent(AddEditExpense.this, TimeAlarm.class)
and I believe that you are passing a unique value i
to the parameter which also should have the same value while creating and canceling pendingintent
.
For more information , please refer my answer HERE
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With