1

I have a service that I use for listening to a socket service. I only need this service to be running whilst the app is in the foreground. However, in production I am seeing several crashes:

IllegalStateException
Not allowed to start service Intent { cmp=com.app.app/.app.sockets.EventHandlingService }: app is in background

Initially, I was calling this in the onCreate of my Activity, and according to some other SO questions, technically the app isn't foregrounded then. So I moved it into the onResume, and yet I still see it happening. This is how it is started:

@Override
protected void onResume() {

    super.onResume();

    Intent serviceIntent = new Intent(this, EventHandlingService.class);
    startService(serviceIntent);
    bindService(serviceIntent, serviceConnection, 0);
}

I am unable to reproduce this crash locally.

Rahul Gaur
  • 1,661
  • 1
  • 13
  • 29
  • Does this answer your question? [Android 8.0: java.lang.IllegalStateException: Not allowed to start service Intent](https://stackoverflow.com/questions/46445265/android-8-0-java-lang-illegalstateexception-not-allowed-to-start-service-inten) – jeprubio Feb 28 '20 at 07:55
  • No, that is about how to actually run a service in the background. My problem is that I only need it in the foreground and in the onResume() I expect the app to be in the foreground, so I don't understand why the new limitations are affecting me. – Feathers McGraw Feb 28 '20 at 08:17
  • why are you doing both startservice and bindservice?. I do not get the use case. – Sekhar Madhiyazhagan Feb 28 '20 at 08:58

0 Answers0