I'm writing something like a reminder for users. Users will set reminders for their events, when the time comes, a repeating alarm will be set to trigger a status bar notification. But the alarm seems non-stop after I selected the notification or cleared the notification. I am not sure where to cancel this repeating alarm. Below are some of the codes: Set up the repeating alarm in my main activity
alarmTime = Calendar.getInstance(); Intent intent = new Intent(this, AlarmReceive.class); PendingIntent sender = PendingIntent.getBroadcast(this, 0, intent, 0); AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); alarmTime.add(Calendar.MINUTE,offset_time); //Schedule the alarm alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, alarmTime.getTimeInMillis(), 30 * 1000, sender);
In my OnReceive method, I just display the notification in status bar and set the flag as FLAG_AUTO_CANCEL
manager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE); // Set the icon, scrolling text and timestamp Notification notification = new Notification(R.drawable.medical, text, System.currentTimeMillis()); PendingIntent contentIntent = PendingIntent.getActivity(context, 0, i, 0); notification.flags = Notification.FLAG_AUTO_CANCEL; manager.notify(R.string.service_text, notification);
How can I stop the alarm when the user selects the notification or clears it?
Open your phone's Clock app . At the bottom, tap Alarm. On the alarm you want, tap the Down arrow . Cancel: To cancel an alarm scheduled to go off in the next 2 hours, tap Dismiss.
In order to delete : AlarmManager alarmManager = (AlarmManager) getSystemService(Context. ALARM_SERVICE); Intent myIntent = new Intent(getApplicationContext(), SessionReceiver. class); PendingIntent pendingIntent = PendingIntent.
At the top of the main panel you should see an option to Add alarm. Tap this and you'll be presented with a time in the top half of the screen, with various settings in the lower half. Scroll the hours up or down until you reach the one you want, then repeat the process with the minutes.
Call cancel()
on AlarmManager
with an equivalent PendingIntent
to the one you used with setRepeating()
:
Intent intent = new Intent(this, AlarmReceive.class); PendingIntent sender = PendingIntent.getBroadcast(this, 0, intent, 0); AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); alarmManager.cancel(sender);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With