1

Since Android Oreo you cannot start a service when the app is not in foreground. In my app I start a service in the activity's onStart-method. This works perfectly fine most of the times. However, from time to time an IllegalStateException is thrown saying that the application is trying to start a service while in the background:

    java.lang.IllegalStateException: Not allowed to start service Intent { act=ui cmp=com.someapp/.services.ConnectionService }: app is in background uid UidRecord{8d70361 u0a255 TPSL bg:+3m12s948ms idle change:cached procs:1 proclist:20368, seq(0,0,0)}
    at android.app.ContextImpl.startServiceCommon(ContextImpl.java:1666)
    at android.app.ContextImpl.startService(ContextImpl.java:1611)
    at android.content.ContextWrapper.startService(ContextWrapper.java:677)
    at com.someapp.ui.SomeActivity.connectToBackend(SomeActivity.java:62)
    at com.someapp.ui.SomeActivity.onStart(SomeActivity.java:55)
    at com.someapp.ui.MainActivity.onStart(MainActivity.kt:228)
    at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1391)
    at android.app.Activity.performStart(Activity.java:7348)
    at android.app.ActivityThread.handleStartActivity(ActivityThread.java:3131)
    at android.app.servertransaction.TransactionExecutor.performLifecycleSequence(TransactionExecutor.java:180)
    at android.app.servertransaction.TransactionExecutor.cycleToPath(TransactionExecutor.java:165)
    at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:142)
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:70)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1947)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:214)
    at android.app.ActivityThread.main(ActivityThread.java:7032)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:964)

I start the service as follows:

@Override
protected void onStart() {
    if (BuildConfig.DEBUG_MODE) Log.d(TAG, "activity started");
    super.onStart();
    connectToBackend();
}

void connectToBackend() {
    Intent intent = new Intent(this, ConnectionService.class);
    intent.setAction("ui");
    startService(intent);
    getApplicationContext().bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
}

I noticed that this only happens when I lock and then unlock the phone while in the app. I cannot reproduce it consistently.

Does somebody have the same issue?

The device I'm using is a Samsung Galaxy S10e.

Jeyhey
  • 490
  • 3
  • 16
  • Code needs to be added – Pankaj Kumar Mar 23 '19 at 22:28
  • Have you check https://stackoverflow.com/questions/46445265/android-8-0-java-lang-illegalstateexception-not-allowed-to-start-service-inten – Pankaj Kumar Mar 23 '19 at 22:32
  • As I commented on [here](https://stackoverflow.com/questions/55263146/crash-starting-service-when-observing-processlifecycleowner-on-start#comment97257634_55263146), why do you have a service? Services are for those occasions where you need to do work when your UI is *not* in the foreground. What value is a service giving you that cannot be met by an ordinary Java/Kotlin object? – CommonsWare Mar 23 '19 at 22:33
  • The app is a messaging app so most of the time its UI is not in the foreground. I use the service to take care of the connection to the server. A service is useful for incoming fcm messages that start the service. Also, when the user leaves the app while in a VoIP call the service is needed to handle the connection while in the background. – Jeyhey Mar 23 '19 at 23:14

1 Answers1

-1

For post Nougat devices, use startForegroundService, and for pre Oreo devices, use startService

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    startForegroundService(intent);
} else {
    startService(intent);
}
Anirudh Gupta
  • 573
  • 5
  • 12
  • 1
    Nobody wants to see a foreground notification when entering an app. Also, the idea of the foreground service is to show the user that the app is doing something in the background, not to be started whenever a user enters the app. – Jeyhey Mar 23 '19 at 23:58
  • I just mentioned the restrictions imposed on Oreo devices, and the only possible solution for your question. – Anirudh Gupta Apr 20 '19 at 14:59
  • The point is that the error occurs although the app is in the foreground as the service is bound in onStart. Apparently there is some race condition occurring. There are at least two quick solutions or better said work arounds possible: (i) start the service in onResume (not tested); (i) delay starting the service by 200ms (what I ended up implementing). – Jeyhey Apr 22 '19 at 16:28