BootReceiver
override fun onReceive(context: Context, intent: Intent) {
if (intent.action == Intent.ACTION_BOOT_COMPLETED) {
val array = MyDatabase(context).getAllAlarm()
val receiver = Intent(context, BootReceiver::class.java)
receiver.action = "ALARM_SETTING"
val trigger = Calendar.getInstance()
for (i in array) {
trigger.time = SimpleDateFormat("yyyy/MM/dd hh:mm", Locale.getDefault()).parse(i.time)!!
receiver.putExtra("trigger", trigger.timeInMillis)
AlarmUtil.setAlarm(context, receiver, i.id, trigger.timeInMillis)
}
} else (intent.action == "ALARM_SETTING") {
val trigger = intent.getLongExtra("trigger", 0)
NotificationUtil.sendNotification(context, name, id, trigger)
}
}
AlarmUtil
class AlarmUtil {
companion object {
fun setAlarm(context: Context, intent: Intent, id: Int, calendar: Long) {
val alarmManager = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager
val pendingIntent = PendingIntent.getBroadcast(context.applicationContext, id, intent, PendingIntent.FLAG_UPDATE_CURRENT)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, calendar, pendingIntent)
} else {
alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar, pendingIntent)
}
}
}
}
As you can see, when the mobile phone reboots, this code brings the alarm array stored in SQLiteDatabase and makes the alarm sound. If you look at setAlarm() in AlarmUtil, I wrote a code that makes it cry at the time specified in AlarmManager. The problem is that all the alarms stored in the array will be cleared at once regardless of the time specified in the alarm array (I implemented it as Notification). Isn't it normal to cry at the calendar time you put in the second argument of setExactAndAllowWhileIdle or setExact? Why do all the alarms cry at once as soon as the phone turns on again?
[EDIT]
The following picture is a situation where the phone is turned off and on after setting the alarm to ring at 02:22. The system time is 2020/11/02 02:21, but the trigger time I set to ring is 2020/11/02 02:22. In other words, the alarm goes off as soon as the time runs out. I want to take a log and show it, but the moment I turn off my phone, the app dies, so I couldn't see the log.

Hi you are passing older time as trigger time. You should pass time in the future. Check db time if it is less than now() than add a day