'How to set multiple alarms in kotlin?

so I'm new to coding and I'm making an app for Muslims prayer times and I want an alarms to be set repeatedly on these times I tried to many things but It doesn't works!

I have a praytimes class and if anyone wants it I'll put it

I will be more thankful, if you could help me on how to set an alarm for 1 prayer so I could do the rest.

Main Activity

    val latitude = 30.354802
    val longitude = 42.2461069
    val timezonoffset = 3.0
    val timeZoneId = "Middle East/Alowiqila"
    val prayTimes = PrayTimes()
    prayTimes.timeFormat = prayTimes.time12//
    prayTimes.calcMethod = prayTimes.makkah// Muslim World League (MWL)
    prayTimes.asrJuristic = prayTimes.hanafi// Shafii (standard)
    prayTimes.adjustHighLats = prayTimes.angleBased
    val offsets = intArrayOf(0, 0, 0, 0, 0, 0, 0) // {Fajr,Sunrise,Dhuhr,Asr,Sunset,Maghrib,Isha}
    prayTimes.tune(offsets)
    val cal = Calendar.getInstance(TimeZone.getTimeZone(timeZoneId))
    cal.time = Date()
    val times = prayTimes.getPrayerTimes(cal, latitude, longitude, timezonoffset)
    println("prayer times for Alowiqila")
    System.out.println("Fajr : " + times.get(0))
    System.out.println("Sunrise : " + times.get(1))
    System.out.println("Duhr : " + times.get(2))
    System.out.println("Asr : " + times.get(3))
    System.out.println("Sunset : " + times.get(4))
    System.out.println("Magrib : " + times.get(5))
    System.out.println("Isha : " + times.get(6))


   // I want the Alarm to be set to the code above it

    fun startAlarm(isNotification:Boolean, isRepeat:Boolean) {
        val manager = getSystemService(Context.ALARM_SERVICE) as AlarmManager
        val myIntent= Intent(this@MainActivity, AlarmReceiver::class.java)
        val pendingIntent:PendingIntent
        // SET TIME HERE
        val calendar = Calendar.getInstance()
        calendar.set(Calendar.HOUR_OF_DAY, 15)
        calendar.set(Calendar.MINUTE, 20)
        pendingIntent = PendingIntent.getBroadcast(this, 0, myIntent, 0)
        if (!isRepeat)
            manager.set(AlarmManager.RTC_WAKEUP, SystemClock.elapsedRealtime() + 3000, pendingIntent)
        else
            manager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.timeInMillis, AlarmManager.INTERVAL_DAY, pendingIntent)
    }

AlarmReceiver.kt

and here is my alarm receiver in witch I've added me notifications

class AlarmReceiver : BroadcastReceiver() {

override fun onReceive(context: Context, intent: Intent) {
    val builder = NotificationCompat.Builder(context)

    val myIntent = Intent(context, MainActivity::class.java)
    val pendingIntent = PendingIntent.getActivity(
        context,
        0,
        myIntent,
        FLAG_ONE_SHOT
    )

    builder.setAutoCancel(true)
        .setDefaults(Notification.DEFAULT_ALL)
        .setWhen(System.currentTimeMillis())
        .setContentTitle("موعد الاذان")
        .setContentIntent(pendingIntent)
        .setDefaults(Notification.DEFAULT_LIGHTS or Notification.DEFAULT_SOUND)
        .setContentInfo("Info")

    val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
    notificationManager.notify(1, builder.build())
}


Solution 1:[1]

You have to put different requestCode in pending intent, each alarm manager requires different requestCode. in your mainActivity

pendingIntent = PendingIntent.getBroadcast(this, (0..2147483647).random(), myIntent, 0)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1