我从 firebase 收到这个问题,来自 moto g5,100% 在后台 android 8.1,我正在运行前台服务,这里是 firebase 堆栈跟踪
Fatal Exception: java.util.ConcurrentModificationException
at android.util.ArrayMap.put(ArrayMap.java:523)
at android.os.Bundle.putParcelable(Bundle.java:501)
at android.app.Notification.addFieldsFromContext(Notification.java:2440)
at android.app.Notification.addFieldsFromContext(Notification.java:2433)
at android.app.Notification$Builder.build(Notification.java:4974)
at com.apep.burnedcalories.CountUpTimerService.updateNotification(CountUpTimerService.java:445)
at com.apep.burnedcalories.CountUpTimerService.access$updateNotification(CountUpTimerService.java:43)
at com.apep.burnedcalories.CountUpTimerService$onStartCommand$1.invokeSuspend(CountUpTimerService.kt:335)
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:571)
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:750)
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:678)
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:665)
这是前台服务类的onstartcomand方法,这里我每秒调用updatenotification方法来更新标题,因为它是一个定时器:
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
CoroutineScope(job).launch(Dispatchers.IO) {
while (isBound) {
if (!isBound) break
if(timer_stoped == false){
startChronometer()
timeFlow.value = getTimestamp().also { time ->
if(start_exercise.txt_time2 != time) {
if(mets_i != intervalo_mets || peso != intervalo_peso){
intervalo = elapsedmilis
intervalo_kcal = burnedcalories
intervalo_mets = mets_i
intervalo_peso = peso
}
burnedcalories = intervalo_kcal + (((0.0175 / 60) * mets_i * peso)*((elapsedmilis-intervalo)/1000))
burnedfat = (burnedcalories / 7.7)
if(isguided == true){
if(progress < 100.0){
if(tts_init == true){
tts_init = false
if (lang_supported == true) {
speak_start(remaining_time)
} else speak_start_en(remaining_time)
}
progress = ((burnedcalories-resume_kcal.toDouble())/start_exercise.goalkcal)*100
remaining_time = getTimestamp2(((((start_exercise.goalkcal+resume_kcal.toDouble())- burnedcalories)/((0.0175/60)*start_exercise.mets_i*start_exercise.peso))*1000).toLong())
}else{
progress = 100.0
remaining_time = "00:00:00"
}
if(((100/numero_notis)*notis_done)<=progress.toInt()){
if(numero_notis == notis_done){
if (lang_supported == true) {
speak_end()
} else speak_end_en()
}else{
if (lang_supported == true) {
speak_noti(burnedcalories.toInt().toString(), progress.toInt().toString())
speak_start(remaining_time)
} else{
speak_noti_en(burnedcalories.toInt().toString(), progress.toInt().toString())
speak_start_en(remaining_time)
}
}
notis_done++
val prefs = getSharedPreferences("prefs", MODE_PRIVATE)
val editor = prefs.edit()
editor.putString("burnedcalories", burnedcalories.toString())
editor.putLong("elapsedmilis", elapsedmilis)
editor.putInt("notis_done", notis_done)
editor.apply()
}
}
start_exercise.txt_time2 = time
start_exercise.txt_time = time
updateNotification(time, start_exercise.burnedcalories.toInt().toString(), start_exercise.burnedfat.toInt().toString())
}
}
}else{
pauseChronometer()
}
delay(1000)
}
}
return START_STICKY
}
这是 updatenotification 方法,这是我得到例外的地方:
private fun updateNotification(timestamp: String, caloriasquemadas: String, grasaquemada: String) {
val notificationManager =
getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
val notification = notificationBuilder
.setContentTitle(timestamp)
.setContentText(caloriasquemadas + "KCAL - " + grasaquemada + "G")
.build()
notificationManager.notify(Constants.NOTIFICATION_ID, notification)
}else{
val notification = builder
.setContentTitle(timestamp)
.setContentText(caloriasquemadas + "KCAL - " + grasaquemada + "G")
.build()
notificationManager.notify(Constants.NOTIFICATION_ID, notification)
}
}
这个问题你解决了吗?大家有什么好的解决办法吗