Cancel alarm manager pending intent according to unique android

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; } 
+4
source share
2 answers

You can get the PendingIntent created for the alarm with:

 PendingIntent displayIntent = PendingIntent.getBroadcast(AddEditExpense.this, iUniqueId, intent, PendingIntent.FLAG_NO_CREATE); 

Be sure to use the unique identifier stored in the sqlite database for this Alarm. Now you must cancel PendingIntent in AlarmManager and cancel PendingIntent :

 if(displayIntent != null) { alarmManager.cancel(displayIntent); displayIntent.cancel(); } 
+12
source

The problem is that you did not provide the same intent as with pendingintent cancellation,

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 new Intent() instead of the intent that you used when creating pendingintent for the first time.

so the intention should be the same when you are going to cancel your pendingintent , in your case it will be

  Intent intent = new Intent(AddEditExpense.this, TimeAlarm.class) 

and I believe that you pass the unique value i parameter, which should also have the same value when creating and canceling pendingintent .

For more information, please write my answer HERE

+2
source

Source: https://habr.com/ru/post/1392866/


All Articles