startService() throws java.lang.IllegalStateException

对着背影说爱祢 提交于 2019-12-08 19:28:53

问题


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.


回答1:


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().


来源:https://stackoverflow.com/questions/44310130/startservice-throws-java-lang-illegalstateexception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!