Erreur startForeground après la mise à jour vers Android 8.1 : comment corriger ?
Bonjour à tous ! Je développe une application en Kotlin qui utilise un service en arrière-plan pour suivre la localisation. Après la mise à jour de l'appareil vers Android 8.1 (Oreo), mon service a cessé de fonctionner et plante avec l'erreur `Context.startForegroundService() did not then call Service.startForeground()`. Voici mon code de service :
```kotlin
class LocationService : Service() {
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
val notification = NotificationCompat.Builder(this, "channel_id")
.setContentTitle("Service actif")
.setContentText("Suivi de localisation")
.setSmallIcon(R.drawable.ic_location)
.build()
startForeground(1, notification)
return START_STICKY
}
override fun onBind(intent: Intent?): IBinder? = null
}
```
Et je le lance comme ceci :
```kotlin
val intent = Intent(this, LocationService::class.java)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
No answers yet.
Connectez-vous pour laisser une réponse
Blogs