Search code examples
androidalarmmanagerandroid-pendingintentandroid-alarms

unable cancel PendingIntents - Android


I have programmed an alarm system that synchronizes with the server where are the information.

Sometimes, in the updates on the server some alarms are deleted. Now, it is well removed from the database db4o but I can't cancel pendingIntents already programmed.

Now, I've the following code:

PendingIntent pendingIntent;
public class xxx{

public void updateObjects(){

alarmManager.cancel(pendingIntent);//delete all alarms
(...)
for(...){
    //Update each object ofdb4o with the new object value's.
    (...)
    doIntents(context,mil,obj);
    }
            (...)
    }

public void doIntents(Context context, long mil, ClassObjects obj){
(...)
pendingIntent = PendingIntent.getBroadcast(context, obj.getId(), intent, PendingIntent.FLAG_UPDATE_CURRENT);
(...)
}
}

Can anybody help me to cancel those pendingintents I don't need already?

Thank you!!!


Solution

  • for cancel PendingIntent you have to pass same ID that you passed for set time.

                PendingIntent pendingIntent = PendingIntent.getBroadcast(context, obj.getId(), intent, PendingIntent.FLAG_UPDATE_CURRENT);
                AlarmManager alarmManager = (AlarmManager) context.getSystemService(ALARM_SERVICE);
                alarmManager.cancel(pendingIntent);