Search code examples
androidservicealarmmanagerandroid-pendingintent

Start alarm in broadcastreceiver and turn off in service


I think I have a design issue, but I will ask you guys the question. My applications receives a broadcast in xbroadcastreceiver, i check some conditions and if true, I start a service, say zservice, using Alarm Manager with PendingIntent. Now in zservice, everytime when it is triggered by the alarmmanager, I check some other conditions and based upon a specific condition I need to cancel the alarm, I know alarmManager.cancel(pendingIntent); will cancel the alarm, but my issue is how to get a access to the pendingIntent as it was in the xbroadcastreciever.

I have tried send pendingIntent to service but so far I am not successful so that when I need, I can turn the alarm off. Hope this logic makes sense.


Solution

  • I know alarmManager.cancel(pendingIntent); will cancel the alram, but my issue is how to get a access to the pendingIntent as it was in the xbroadcastreciever.

    Create an Intent with the same routing information (component, action, categories, MIME type) as the original. Create the same type of PendingIntent from this Intent as before (e.g., getService()). Use that PendingIntent with the cancel() call.