同时发出多个警报

时间:2018-03-14 19:42:25

标签: android alarmmanager android-notifications android-pendingintent

我有时间让我们说01:01 am,现在我想先触发两个通知,一个是时间前五分钟,另一个是时间前两分钟。这是我迄今为止所做的代码:

    fun setAlarm() {

   val time: String = "01:01 AM"
            val inFormat3 = SimpleDateFormat("hh:mm")
            val date = inFormat3.parse(time)
            Log.e("Timeis",date.minutes.toString() + " " + date.hours)
         //   val df = SimpleDateFormat("HH:mm")
          /*  val d = df.format(date)*/
            val calNow = Calendar.getInstance()
            val calSet = calNow.clone() as Calendar
            val calSet2 = calNow.clone() as Calendar

           // val cal = Calendar.getInstance()
            calSet.set(Calendar.HOUR_OF_DAY, date.hours)
            calSet.set(Calendar.MINUTE, date.minutes)
            calSet.set(Calendar.SECOND, 0)
            calSet.set(Calendar.MILLISECOND,0)
            calSet2.set(Calendar.HOUR_OF_DAY, date.hours)
            calSet2.set(Calendar.MINUTE, date.minutes)
            calSet2.set(Calendar.SECOND, 0)
            calSet2.set(Calendar.MILLISECOND,0)

            calSet.add(Calendar.MINUTE,-5)
            calSet2.add(Calendar.MINUTE,-2)

            if(calSet.compareTo(calNow) <= 0){
                //Today Set time passed, count to tomorrow
                calSet.add(Calendar.DATE, 1);
            }
            if(calSet2.compareTo(calNow) <= 0){
                //Today Set time passed, count to tomorrow
                calSet2.add(Calendar.DATE, 1);
            }


            val intent = Intent(this, AlarmReceiver::class.java)
            intent.putExtra("NotificationText", "some text")
            val pendingIntent = PendingIntent.getBroadcast(this, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT)
            val PendingIntent2 = PendingIntent.getBroadcast(this,2,intent,PendingIntent.FLAG_UPDATE_CURRENT)
            val alarmManager = this.getSystemService(Context.ALARM_SERVICE) as AlarmManager
            val alarmManager2 =  this.getSystemService(Context.ALARM_SERVICE) as AlarmManager
            alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calSet.getTimeInMillis(), 24*60*60*1000,pendingIntent)
            alarmManager2.setRepeating(AlarmManager.RTC_WAKEUP,calSet2.getTimeInMillis(),24*60*60*1000,PendingIntent2)
}

现在它只是在通知前5分钟而不是2分钟之前触发通知。我在这做错了什么?

提前致谢。

0 个答案:

没有答案