Android 重复报警会导致通知时间不准确
我把重复警报设定在凌晨3:55。假设在中午,下午1:00,我打开应用程序中的报警按钮。我立即收到凌晨3点55分警报的通知。但为什么会这样? 第二天我在准确的时间收到通知 我以毫秒为单位换算闹钟时间。首先获取此格式的字符串Android 重复报警会导致通知时间不准确,android,kotlin,Android,Kotlin,我把重复警报设定在凌晨3:55。假设在中午,下午1:00,我打开应用程序中的报警按钮。我立即收到凌晨3点55分警报的通知。但为什么会这样? 第二天我在准确的时间收到通知 我以毫秒为单位换算闹钟时间。首先获取此格式的字符串 YYYY/MM/dd“HH:MM:ss。。。然后使用localtime函数以毫秒为单位进行转换。并在am.setRepeating方法中设置此毫秒时间 val d: DateFormat = SimpleDateFormat("yyyy/MM/dd"
YYYY/MM/dd“HH:MM:ss。。。然后使用localtime函数以毫秒为单位进行转换。并在am.setRepeating方法中设置此毫秒时间
val d: DateFormat = SimpleDateFormat("yyyy/MM/dd")
val currenttime: DateFormat = SimpleDateFormat("HH:mm:ss")
val timeformate: DateFormat = SimpleDateFormat("hh:mm")
var time = timeformate.parse(singletime)
//alarm_localtime
val now: String = d.format(Date())
//current_localtime
// var currenttim = now + " " + currenttime.format(Date())
// var current_local_time = localtime(currenttim)
var alarmtime = (now + " " + singletime)
var alarm_local_Time = localtime(alarmtime)
val notificationmassage = Intent(applicationContext, Notificationmassage::class.java)
notificationmassage.putExtra("time", timeformate.format(time));
val pendingintent = PendingIntent.getBroadcast(
mContext,
pos,
notificationmassage,
PendingIntent.FLAG_UPDATE_CURRENT
)
val am = mContext.getSystemService(Context.ALARM_SERVICE) as AlarmManager
//Log.e("currenttime",Calendar.getInstance().getTimeInMillis().toString() )
// Log.e("alarmttime",alarm_local_Time.toString() )
am.setRepeating(
AlarmManager.RTC_WAKEUP,
alarm_local_Time,
AlarmManager.INTERVAL_DAY,
pendingintent
)
@RequiresApi(Build.VERSION_CODES.O)
fun localtime(time: String): Long {
var local_time =
LocalDateTime.parse(time.replace(" ", "T").replace("/", "-"))
.atZone(ZoneId.systemDefault())
.toInstant()
.toEpochMilli()
return local_time
}
当您将其设置为当前日期,并在下午1:00设置为已经过去的凌晨3:55时,它将触发该通知。
如果时间戳早于当前时间,请尝试在日期中添加一天。您是否可以确认报警时间(以毫秒为单位)是否正确?是的。我在日历中设置了相同的小时和分钟,比如cal[Calendar.hour]=3,cal[Calendar.MINUTE]=55。。获取相同的毫秒数据。第二天警报通知的准确时间是3:55。我猜,由于您将其设置为当前日期,下午1:00设置为已经过去的3:55,因此它将触发该通知。如果时间戳比当前时间早,请尝试在日期中添加一天。非常感谢。我做到了。如果(报警本地时间<当前本地时间){报警本地时间=报警本地时间+24*60*60*1000;}