Creating Multiple Alarms Using AlarmManager Alarms

Asked 2 years ago, Updated 2 years ago, 85 views

Hi, everyone. I'm creating an alarm on Android using AlarmManager and PengingIntent. The alarm goes off three times every morning, lunch, and dinner The alarm is not working properly. The alarm may sound a long time later (not an error of 1 to 2 minutes, but a difference of more than 10 minutes), or the morning/lunch/dinner alarm may sound all at once in the morning.

Here's the rough way Activity receives each time with 3 TimePickerDialogs and passes it to the AlarmReceiver. The addAlarm() function in the AlarmReceiver then receives the value and adds the alarm. Registered alarms run the onReceive() function of the AlarmReceiver when the time comes OnReceive displays the AlarmActivity on the screen.

Here's the code.

AlarmReceiver.java's addAlarm()

Intent intent = new Intent(con, AlarmReceiver.class);
PendingIntent alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
Calendar cal = new Gregorian Calendar(); // Calendar object for setting the time
cal.set (2016, 7, 27, morning Hour, morning Minute, 0); // Hour and Minute are imported into TimePickerDialog
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), AlarmManager.INTERVAL_DAY, alarmIntent); // Set alarm that repeats once a day

intent = new Intent(con, AlarmReceiver.class);
alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
cal.set (2016, 7, 27, launchHour, launchMinute, 0); // Imported into TimePickerDialog
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), AlarmManager.INTERVAL_DAY, alarmIntent);

intent = new Intent(con, AlarmReceiver.class);
alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
cal.set (2016, 7, 27, dinnerHour, dinnerMinute, 0); // Imported into TimePickerDialog
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), AlarmManager.INTERVAL_DAY, alarmIntent);

The variables (breakfast, lunch, and dinner time/minute) all had different values. The use of AlarmManager is more difficult than I thoughtㅜ<

alarmmanager intent android-pendingintent

2022-09-21 18:52

1 Answers

The setInexactRepeating() function is a less accurate alarm setting function, as the name implies. You can understand that it is used to set alarms that allow errors. In API 19, you could set up an exact repeat alarm using the setRepeating() function, but from API 19, even this function does not guarantee accurate time. Therefore, the setExact() function must be used to repeat the exact alarm, and the part of the iteration (the next alarm trigger time) must be implemented directly. Please check the document below for related information.

Note: as of API 19, all repeating alarms are inexact. If your application needs precise delivery times then it must use one-time exact alarms, rescheduling each time as described above. Legacy applications whose targetSdkVersion is earlier than API 19 will continue to have all of their alarms, including repeating alarms, treated as exact.

Please refer to the following link for implementations that you can refer to.

In addition, I think the following article, which gives you an overview of the alarm, will be helpful. (Korean)

Previous time than it is now time

and trigger an alarm, set to immediately executed. The phenomenom is speculation in this case, and become a morning / afternoon / evening. See the following link is for this part of treatment.


2022-09-21 18:52

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.