StartService () throws java.lang.IllegalStateException

From onCreate() my activity,

I am trying to start the service through the following code:

 Intent intent = new Intent(); intent.setClassName(SERVICE_PKG_NAME, SERVICE_NAME); context.startService(intent); //context = MainActivity.this 

However, I get the following exception:

java.lang.IllegalStateException: not allowed to start the service Intent (service-name): the application is in the background

Any idea what could be causing this? Stuck on it for hours.

+5
source share
1 answer

In cases involving the need to call the background application service from the foreground application, we can follow the sequence:

  • call bindService () first
  • after OnServiceConnected () due to bind call
  • call StartService ().
+1
source

Source: https://habr.com/ru/post/1268440/


All Articles