9

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.

4

1 回答 1

3

对于需要从前台应用调用后台应用服务的情况,我们可以按照以下顺序进行:

  • 先调用 bindService()
  • 由于绑定调用,在 OnServiceConnected() 之后
  • 调用 StartService()。
于 2017-06-07T05:29:53.327 回答