You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WorkManager 2.8.1出现ForegroundServiceStartNotAllowedException崩溃求助

WorkManager 2.8.1 频繁触发ForegroundServiceStartNotAllowedException崩溃

崩溃栈信息

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)

当前代码处理

我们在Worker中对setForeground调用做了try-catch处理,仅捕获IllegalStateException:

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

实际捕获到的异常日志

日志显示ForegroundServiceStartNotAllowedException被捕获:

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)

疑问

请问这是我们遗漏了异常处理逻辑,还是WorkManager本身就不应该抛出这个异常?补充说明:我们尚未针对API 34进行适配。


内容的提问来源于stack exchange,提问作者bernardo.g

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.21 00:50:56