ForegroundServiceStartNotAllowedException in WorkManager

We’re on version 2.8.1 of WorkManager, and we’re getting a significant ammount of crashes like the following:

Fatal Exception: android.app.ForegroundServiceStartNotAllowedException: Service.startForeground() not allowed due to mAllowStartForeground false: service com.{app name}.android/androidx.work.impl.foreground.SystemForegroundService
at android.app.ForegroundServiceStartNotAllowedException$1.createFromParcel(ForegroundServiceStartNotAllowedException.java:54)
at android.app.ForegroundServiceStartNotAllowedException$1.createFromParcel(ForegroundServiceStartNotAllowedException.java:50)
at android.os.Parcel.readParcelableInternal(Parcel.java:5016)
at android.os.Parcel.readParcelable(Parcel.java:4998)
at android.os.Parcel.createExceptionOrNull(Parcel.java:3178)
at android.os.Parcel.createException(Parcel.java:3167)
at android.os.Parcel.readException(Parcel.java:3150)
at android.os.Parcel.readException(Parcel.java:3092)
at android.app.IActivityManager$Stub$Proxy.setServiceForeground(IActivityManager.java:6960)
at android.app.Service.startForeground(Service.java:863)
at androidx.work.impl.foreground.SystemForegroundService$Api31Impl.startForeground(SystemForegroundService.java:194)
at androidx.work.impl.foreground.SystemForegroundService$1.run(SystemForegroundService.java:130)
at android.os.Handler.handleCallback(Handler.java:959)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loopOnce(Looper.java:232)
at android.os.Looper.loop(Looper.java:317)
at android.app.ActivityThread.main(ActivityThread.java:8592)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:580)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:878)

In the Worker, we’re calling setForeground within a try-catch block, and catching the IllegalStateException:

try {
    setForeground(createForegroundInfoSafely(notificationId, notification))
} catch (e: IllegalStateException) {
    Logger.SaveEx(
        e,
        "$TAG Exception on call to setForeground() in createOrUpdateInitialNotification()" +
                " with progress $progress."
    )
}

The logs show the exception being caught in the 1st line.

Tue Jul 09 2024 10:01:52 GMT-0300 (Brasilia Standard Time) | [DownloadWorker] Exception on call to setForeground() in createOrUpdateInitialNotification() with progress 100.
Tue Jul 09 2024 10:01:52 GMT-0300 (Brasilia Standard Time) | An exception occurred: android.app.ForegroundServiceStartNotAllowedException: startForegroundService() not allowed due to mAllowStartForeground false: service com.{app name}.android/androidx.work.impl.foreground.SystemForegroundService
Tue Jul 09 2024 10:01:52 GMT-0300 (Brasilia Standard Time) | Stacktrace: android.app.ForegroundServiceStartNotAllowedException: startForegroundService() not allowed due to mAllowStartForeground false: service com.{app name}.android/androidx.work.impl.foreground.SystemForegroundService   at android.app.ForegroundServiceStartNotAllowedException$1.createFromParcel(ForegroundServiceStartNotAllowedException.java:54)  at android.app.ForegroundServiceStartNotAllowedException$1.createFromParcel(ForegroundServiceStartNotAllowedException.java:50)  at android.os.Parcel.readParcelable(Parcel.java:3334)   at android.os.Parcel.createExceptionOrNull(Parcel.java:2421)    at android.os.Parcel.createException(Parcel.java:2410)  at android.os.Parcel.readException(Parcel.java:2393)    at android.os.Parcel.readException(Parcel.java:2335)    at android.app.IActivityManager$Stub$Proxy.startService(IActivityManager.java:5974)     at android.app.ContextImpl.startServiceCommon(ContextImpl.java:1848)    at android.app.ContextImpl.startForegroundService(ContextImpl.java:1824)    at android.content.ContextWrapper.startForegroundService(ContextWrapper.java:781)   at androidx.core.content.ContextCompat$Api26Impl.startForegroundService(SourceFile:1)   at androidx.core.content.ContextCompat.startForegroundService(SourceFile:7)     at androidx.work.impl.Processor.startForeground(SourceFile:82)  at androidx.work.impl.utils.WorkForegroundUpdater$1.run(SourceFile:39)  at androidx.work.impl.utils.SerialExecutorImpl$Task.run(SourceFile:3)   at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:644)  at java.lang.Thread.run(Thread.java:1012) Caused by: android.os.RemoteException: Remote stack trace:    at com.android.server.am.ActiveServices.startServiceLocked(ActiveServices.java:726)     at com.android.server.am.ActiveServices.startServiceLocked(ActiveServices.java:651)     at com.android.server.am.ActivityManagerService.startService(ActivityManagerService.java:12121)     at android.app.IActivityManager$Stub.onTransact(IActivityManager.java:2521)     at com.android.server.am.ActivityManagerService.onTransact(ActivityManagerService.java:2534)

Am I missing something, or is this exception not supposed to be thrown by WorkManager?

4

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật