1

I'm investigating several IllegalStateExceptions only on Android 9 devices which I cannot reproduce. First I thought it depends on the restriction starting services in background, but 1st they were introduced in Android 8 and I dont get these exceptions on Android 8 and 2nd the stacktrace shows the service was started from onCreate, so it was in foreground, isnt it?

The full stacktrace:

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)

The line doStartService (ServiceManager.java:121) is:

mActivity.startService(new Intent(mActivity, mServiceClass));

Can anyone imagine whats going on there?

Any help would be much appreciated. Thank you!

Andrii Artamonov
  • 622
  • 8
  • 15
Max Power
  • 169
  • 10

1 Answers1

1
  Intent serviceIntent = new Intent(context,Service.class);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                startForegroundService(serviceIntent);
            }
            else
            {
                startService(serviceIntent);
            }

"If your app targets API level 26 or higher, the system imposes restrictions on using or creating background services unless the app itself is in the foreground. If an app needs to create a foreground service, the app should call startForegroundService(). That method creates a background service, but the method signals to the system that the service will promote itself to the foreground. Once the service has been created, the service must call its startForeground() method within five seconds."

https://stackoverflow.com/a/46138653/11982611 answers why it works on Oreo

Eren Tüfekçi
  • 2,463
  • 3
  • 16
  • 35
  • Thank you. But it doesnt work. I found out that Background Execution Limits dont affect bound services, which I use. Right after my startService() bindService() is called. I don't believe in a relation to Execution Limits ... and I have to add that I dont get the exception on Android 9 simulator. – Max Power Jan 22 '20 at 08:19