Getting android.app.ForegroundServiceStartNotAllowedException in Android 12 (SDK 31)如何重现崩溃这个例外是什么,为什么添加它?可能的解决方案
我已将我的应用程序升级到 SDK 31,并开始在后台更新小部件的服务中的应用程序中收到以下崩溃。targetSdkVersion
compileSdkVersion
java.lang.RuntimeException:
at android.app.ActivityThread.handleReceiver (ActivityThread.java:4321)
at android.app.ActivityThread.access$1600 (ActivityThread.java:247)
at android.app.ActivityThread$H.handleMessage (ActivityThread.java:2068)
at android.os.Handler.dispatchMessage (Handler.java:106)
at android.os.Looper.loopOnce (Looper.java:201)
at android.os.Looper.loop (Looper.java:288)
at android.app.ActivityThread.main (ActivityThread.java:7842)
at java.lang.reflect.Method.invoke (Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:548)
at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1003)
Caused by: android.app.ForegroundServiceStartNotAllowedException:
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:3333)
at android.os.Parcel.createExceptionOrNull (Parcel.java:2420)
at android.os.Parcel.createException (Parcel.java:2409)
at android.os.Parcel.readException (Parcel.java:2392)
at android.os.Parcel.readException (Parcel.java:2334)
at android.app.IActivityManager$Stub$Proxy.startService (IActivityManager.java:5971)
at android.app.ContextImpl.startServiceCommon (ContextImpl.java:1847)
at android.app.ContextImpl.startForegroundService (ContextImpl.java:1823)
at android.content.ContextWrapper.startForegroundService (ContextWrapper.java:779)
at android.content.ContextWrapper.startForegroundService (ContextWrapper.java:779)
at com.mypackage.appname.ui.widget.widget_package.WidgetClassName.onUpdate (WidgetClassName.java:48)
at android.appwidget.AppWidgetProvider.onReceive (AppWidgetProvider.java:66)
at com.mypackage.appname.ui.widget.widget_package.WidgetClassName.onReceive (WidgetClassName.java)
at android.app.ActivityThread.handleReceiver (ActivityThread.java:4312)
at android.app.ActivityThread.access$1600 (ActivityThread.java:247)
at android.app.ActivityThread$H.handleMessage (ActivityThread.java:2068)
at android.os.Handler.dispatchMessage (Handler.java:106)
at android.os.Looper.loopOnce (Looper.java:201)
at android.os.Looper.loop (Looper.java:288)
at android.app.ActivityThread.main (ActivityThread.java:7842)
at java.lang.reflect.Method.invoke (Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:548)
at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1003)
Caused by: android.os.RemoteException:
at com.android.server.am.ActiveServices.startServiceLocked (ActiveServices.java:691)
at com.android.server.am.ActiveServices.startServiceLocked (ActiveServices.java:616)
at com.android.server.am.ActivityManagerService.startService (ActivityManagerService.java:11839)
at android.app.IActivityManager$Stub.onTransact (IActivityManager.java:2519)
at com.android.server.am.ActivityManagerService.onTransact (ActivityManagerService.java:2498)
另外,如果您使用的是 Firebase Crashlytics 之类的东西,则堆栈跟踪必须如下所示 ->
Caused by android.app.ForegroundServiceStartNotAllowedException: startForegroundService() not allowed due to mAllowStartForeground false: service com.mypackage.appname/.ui.widget.widget_package.MyForegroundServiceName
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:3333)
at android.os.Parcel.createExceptionOrNull(Parcel.java:2420)
at android.os.Parcel.createException(Parcel.java:2409)
at android.os.Parcel.readException(Parcel.java:2392)
at android.os.Parcel.readException(Parcel.java:2334)
at android.app.IActivityManager$Stub$Proxy.startService(IActivityManager.java:5971)
at android.app.ContextImpl.startServiceCommon(ContextImpl.java:1847)
at android.app.ContextImpl.startForegroundService(ContextImpl.java:1823)
at android.content.ContextWrapper.startForegroundService(ContextWrapper.java:779)
at android.content.ContextWrapper.startForegroundService(ContextWrapper.java:779)
at com.mypackage.appname.ui.widget.widget_package.WidgetClassName.onUpdate(WidgetClassName.java:48)
at android.appwidget.AppWidgetProvider.onReceive(AppWidgetProvider.java:66)
at com.mypackage.appname.ui.widget.widget_package.WidgetClassName.onReceive(WidgetClassName.java)
at android.app.ActivityThread.handleReceiver(ActivityThread.java:4312)
at android.app.ActivityThread.access$1600(ActivityThread.java:247)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2068)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loopOnce(Looper.java:201)
at android.os.Looper.loop(Looper.java:288)
at android.app.ActivityThread.main(ActivityThread.java:7842)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
我正在添加重现此问题的方法,并修复了此问题,因为我在搜索StackOverflow时没有找到有关此问题的任何文档。