我们使用的是
2.8.1
的 WorkManager
版本,我们遇到了大量崩溃,如下所示:
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
中,我们在 try-catch 块中调用 setForeground
,并捕获 IllegalStateException
:
try {
setForeground(createForegroundInfoSafely(notificationId, notification))
} catch (e: IllegalStateException) {
Logger.SaveEx(
e,
"$TAG Exception on call to setForeground() in createOrUpdateInitialNotification()" +
" with progress $progress."
)
}
日志显示第一行捕获了异常。
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
抛出?
我猜你最近更新到了 targetSdk 34,现在你需要指定前台服务类型(对于 WorkManagers 内部服务也是如此,这是你的错误来源)
https://developer.android.com/about/versions/14/changes/fgs-types-required
因此,请在上面的链接上找到合适的服务类型(取决于您的服务从事什么类型的工作)。
然后,例如使用“dataSync”服务类型,您将需要:
<service android:name="androidx.work.impl.foreground.SystemForegroundService" android:foregroundServiceType="dataSync" />
< uses-permission android:name="android.permission.FOREGROUND_SERVICE_DATA_SYNC"/>
private fun createForegroundInfo(): ForegroundInfo { return if(SDK_INT>= Q) { ForegroundInfo(15, sendNotification(), ServiceInfo.FOREGROUND_SERVICE_TYPE_DATA_SYNC) }else{ ForegroundInfo(15, sendNotification()) } }