为什么我的 alarmmanager 没有在指定时间关闭?
Why is my alarmmanager not going off at specified time?
每次按下按钮时都会调用 setAlarm。每次我这样做时,无论如何都会在 5 秒后出现“测试”消息。更改 .setExact() 的第二个参数似乎不会影响闹钟何时响起。
在MainActivity中设置Alarm方法
private fun setAlarm (cal : Calendar){
val alarmManager :AlarmManager = getSystemService(Context.ALARM_SERVICE) as AlarmManager
val intent = Intent (this, AlertReceiver().javaClass)
val pendingIntent : PendingIntent = PendingIntent.getBroadcast(this, 1, intent, 0)
//
alarmManager.setExact(AlarmManager.RTC_WAKEUP, 100000, pendingIntent)
}
AlertReceiver
class AlertReceiver: BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
Toast.makeText(context, "test", Toast.LENGTH_SHORT).show()
}
}
你应该试试这个
private fun setAlarm (cal : Calendar){
val calendar = Calendar.getInstance()
calendar.apply {
set(Calendar.HOUR_OF_DAY, 6)
set(Calendar.MINUTE, 30)
set(Calendar.SECOND, 0)
}
val alarmManager :AlarmManager = getSystemService(Context.ALARM_SERVICE) as AlarmManager
val intent = Intent (this, AlertReceiver().javaClass)
val pendingIntent : PendingIntent = PendingIntent.getBroadcast(this, 1, intent, 0)
//
alarmManager.set( AlarmManager.RTC_WAKEUP,
calendar.timeInMillis,
pendingIntent)
}
每次按下按钮时都会调用 setAlarm。每次我这样做时,无论如何都会在 5 秒后出现“测试”消息。更改 .setExact() 的第二个参数似乎不会影响闹钟何时响起。
在MainActivity中设置Alarm方法
private fun setAlarm (cal : Calendar){
val alarmManager :AlarmManager = getSystemService(Context.ALARM_SERVICE) as AlarmManager
val intent = Intent (this, AlertReceiver().javaClass)
val pendingIntent : PendingIntent = PendingIntent.getBroadcast(this, 1, intent, 0)
//
alarmManager.setExact(AlarmManager.RTC_WAKEUP, 100000, pendingIntent)
}
AlertReceiver
class AlertReceiver: BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
Toast.makeText(context, "test", Toast.LENGTH_SHORT).show()
}
}
你应该试试这个
private fun setAlarm (cal : Calendar){
val calendar = Calendar.getInstance()
calendar.apply {
set(Calendar.HOUR_OF_DAY, 6)
set(Calendar.MINUTE, 30)
set(Calendar.SECOND, 0)
}
val alarmManager :AlarmManager = getSystemService(Context.ALARM_SERVICE) as AlarmManager
val intent = Intent (this, AlertReceiver().javaClass)
val pendingIntent : PendingIntent = PendingIntent.getBroadcast(this, 1, intent, 0)
//
alarmManager.set( AlarmManager.RTC_WAKEUP,
calendar.timeInMillis,
pendingIntent)
}