4

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;
        }
shyam
  • 1,276
  • 4
  • 25
  • 51
  • You can also check my answer [here](http://stackoverflow.com/questions/8877365/cancelling-a-single-alarm-when-you-have-multiple-alarms/8877875#8877875) – Lalit Poptani Jan 25 '12 at 10:24

2 Answers2

12

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();  
}
Janusz
  • 187,060
  • 113
  • 301
  • 369
  • hi Jansz ..how to cancel it in broadcast recievr's class? – shyam Jan 25 '12 at 09:10
  • It is the same in every class. Get the uniqueId of the Intent and recreate the Intent you used to activate the PendingIntent and PendingIntent.getBroadCast allows you to retrieve the PendingIntent from everywhere. – Janusz Jan 25 '12 at 10:18
  • i am trying to cancel alarm manager on onContextItemSelected on list position click and i saved uniqueId in sqlite databse which i send with when i create alarm..now i am trying to use same id inside pending intent but failed to cancel pls refer to my code...above .... – shyam Jan 25 '12 at 12:58
2

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

Community
  • 1
  • 1
dharmendra
  • 7,835
  • 5
  • 38
  • 71