I schedule the task and add an alarm manager according to the date and time, and the scheduled task adds to the list. When I add a task, it is also added to the sqlite database and assigns a unique identifier for the pendinng intent to the alarm manager.
Now, if I want to reject an alarm, then if I delete a line from the list, then I also want to reject this alarm. I can delete a row from the database, but how to reject the alarm set for this 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 also see here: 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; }
source share