来自前台(!)应用程序的Android 9(!)非法状态异常startService

来自前台(!)应用程序的Android 9(!)非法状态异常startService,android,service,android-9.0-pie,Android,Service,Android 9.0 Pie,我正在调查几个仅在Android 9设备上存在的非法状态异常,我无法复制这些异常。首先,我认为这取决于在后台启动服务的限制,但第一,它们是在Android 8中引入的,我在Android 8上没有发现这些异常,第二,stacktrace显示服务是从onCreate启动的,所以它在前台,不是吗 完整堆栈跟踪: java.lang.RuntimeException: at android.app.ActivityThread.performLaunchActivity (ActivityThrea

我正在调查几个仅在Android 9设备上存在的非法状态异常,我无法复制这些异常。首先,我认为这取决于在后台启动服务的限制,但第一,它们是在Android 8中引入的,我在Android 8上没有发现这些异常,第二,stacktrace显示服务是从onCreate启动的,所以它在前台,不是吗

完整堆栈跟踪:

java.lang.RuntimeException: 
at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:3430)
at android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:3614)
at android.app.servertransaction.LaunchActivityItem.execute (LaunchActivityItem.java:86)
at android.app.servertransaction.TransactionExecutor.executeCallbacks (TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute (TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage (ActivityThread.java:2199)
at android.os.Handler.dispatchMessage (Handler.java:112)
at android.os.Looper.loop (Looper.java:216)
at android.app.ActivityThread.main (ActivityThread.java:7625)
at java.lang.reflect.Method.invoke (Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:524)
at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:987)

caused by java.lang.IllegalStateException:

at android.app.ContextImpl.startServiceCommon (ContextImpl.java:1810)
at android.app.ContextImpl.startService (ContextImpl.java:1765)
at android.content.ContextWrapper.startService (ContextWrapper.java:664)
at com.max.power.ServiceManager.doStartService (ServiceManager.java:121)
at com.max.power.ServiceManager.start (ServiceManager.java:82)
at com.max.power.ActivityLogin.setupSocketService (ActivityLogin.java:259)
at com.max.power.ActivityLogin.onCreate (ActivityLogin.java:156)
at android.app.Activity.performCreate (Activity.java:7458)
at android.app.Activity.performCreate (Activity.java:7448)
at android.app.Instrumentation.callActivityOnCreate (Instrumentation.java:1286)
at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:3409)
doStartService(ServiceManager.java:121)行是:

有人能想象那里发生了什么吗

任何帮助都将不胜感激。谢谢大家!

  Intent serviceIntent = new Intent(context,Service.class);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                startForegroundService(serviceIntent);
            }
            else
            {
                startService(serviceIntent);
            }
“如果您的应用程序的目标API级别为26或更高,则系统会对使用或创建后台服务施加限制,除非应用程序本身位于前台。如果应用程序需要创建前台服务,则应用程序应调用startForegroundService()。该方法创建了一个后台服务,但该方法向系统发出信号,表示该服务将自己提升到前台。一旦创建了该服务,该服务必须在五秒钟内调用其startForeground()方法。“


回答为什么它在奥利奥上有效

谢谢。但它不起作用。我发现后台执行限制不会影响我使用的绑定服务。就在调用我的startService()bindService()之后。我不相信与执行限制有关。。。我必须补充一点,我在Android 9模拟器上没有得到例外。
  Intent serviceIntent = new Intent(context,Service.class);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                startForegroundService(serviceIntent);
            }
            else
            {
                startService(serviceIntent);
            }