Fehler startForeground nach Update auf Android 8.1: App stürzt mit ForegroundServiceDidNotStartInTimeException ab
Hallo zusammen! Ich bin ein Anfänger-Android-Entwickler in Kotlin. Nach dem Update meines Geräts auf Android 8.1 (Oreo) stürzt meine App beim Starten eines Hintergrunddienstes mit einem Fehler ab. Früher auf Android 7.0 hat alles einwandfrei funktioniert.
Hier ist mein Service-Code:
```kotlin
class MyForegroundService : Service() {
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
val notification = Notification.Builder(this, "my_channel")
.setContentTitle("Service läuft")
.setContentText("Download wird ausgeführt...")
.setSmallIcon(R.drawable.ic_notification)
.build()
startForeground(1, notification)
// ... lange Hintergrundaufgabe
return START_STICKY
}
override fun onBind(intent: Intent?): IBinder? = null
}
```
Und so starte ich den Service aus der Activity:
```kotlin
startService(Intent(this, MyForegroundService::class.java))
```
Der Fehler in den Logs: `android.app.ForegroundServiceDidN