Reputation: 1528
I'm making an alarm app and wondering if it is needed to implement a service to maintain an alarm to ring in the future. Can't 'PendingIntent' do the job? We can assign every alarm time a unique ID and it should ring.
The only case where I see a service is needed is when the phone reboots and we need a service to respond to the reboot broadcast by setting the alarms again as I assume rebooting will destroy the 'PendingIntent'.
Upvotes: 0
Views: 33
Reputation: 55350
I assume you're referring to using AlarmManager
.
Yes, you can supply a PendingIntent
created from an Activity
, and it should work. This is not recommended mainly for UX (and not technical) reasons -- unless your app is a timer, abruptly interrupting what the user is doing can be very annoying. Hence, a service is usually tasked with showing a notification when the alarm "sounds", and tapping on this alarm opens the activity.
As you indicated, you need to listen for BOOT_COMPLETED
because alarms are wiped out when the device is restarted.
Upvotes: 1