Skip to content

Commit 03c32b5

Browse files
yhn112oliexdev
andauthored
Stop the sync service when its foreground-service time runs out (#40)
* Stop the sync service when its foreground-service time runs out Since Android 15, dataSync foreground services may run for 6 hours per 24 hours in the background. When the budget is used up the system calls Service.onTimeout(), and a service that has not stopped a few seconds later crashes the app with a RemoteServiceException. SyncService did not override onTimeout(), so a backend that hangs long enough would crash the app. Stop the service there, as the platform requires. * Delete src/app/src/test/java/com/health/openscale/sync/core/service/SyncServiceTimeoutTest.kt --------- Co-authored-by: OliE <olie.xdev@googlemail.com>
1 parent 8a216f6 commit 03c32b5

1 file changed

Lines changed: 11 additions & 0 deletions

File tree

  • src/app/src/main/java/com/health/openscale/sync/core/service

‎src/app/src/main/java/com/health/openscale/sync/core/service/SyncService.kt‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,17 @@ class SyncService : Service() {
357357
stopSelf()
358358
}
359359

360+
/**
361+
* Android 15+ lets dataSync foreground services run for 6 hours per 24 hours in the
362+
* background. When that budget runs out the system calls this, and a service that has not
363+
* stopped a few seconds later crashes the app with a RemoteServiceException. A normal sync
364+
* finishes long before that, so this only fires when a backend hangs.
365+
*/
366+
override fun onTimeout(startId: Int, fgsType: Int) {
367+
Timber.w("Foreground service timed out (startId=%d, fgsType=%d) -> stopping", startId, fgsType)
368+
stopServiceCleanly()
369+
}
370+
360371

361372
/** Creates required foreground notification for service */
362373
private fun showNotification() {

0 commit comments

Comments
 (0)