Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

startService() throws java.lang.IllegalStateException

From onCreate() of my Activity,

I am trying to start a service via following code:

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

However, I receive following exception:

java.lang.IllegalStateException: Not allowed to start service Intent (service-name) : app is in background

Any idea what could be reason for this? Stuck on it for few hours now.

like image 890
Sahil Mehta Avatar asked Jun 01 '17 14:06

Sahil Mehta


1 Answers

For cases involving need to invoke service of Background app from a foreground app, We can follow the sequence:

  • call bindService() first
  • after OnServiceConnected() due to bind call
  • call StartService().
like image 128
Sahil Mehta Avatar answered Nov 05 '22 09:11

Sahil Mehta