startService() 抛出 java.lang.IllegalStateException
startService() throws java.lang.IllegalStateException
来自我的 Activity 的 onCreate()
,
我正在尝试通过以下代码启动服务:
Intent intent = new Intent();
intent.setClassName(SERVICE_PKG_NAME, SERVICE_NAME);
context.startService(intent); //context = MainActivity.this
但是,我收到以下异常:
java.lang.IllegalStateException: Not allowed to start service Intent (service-name) : app is in background
知道这可能是什么原因吗?现在坚持了几个小时。
对于需要从前台应用调用后台应用服务的情况,我们可以按照顺序:
- 先调用 bindService()
- 由于绑定调用而在 OnServiceConnected() 之后
- 调用 StartService()。
来自我的 Activity 的 onCreate()
,
我正在尝试通过以下代码启动服务:
Intent intent = new Intent();
intent.setClassName(SERVICE_PKG_NAME, SERVICE_NAME);
context.startService(intent); //context = MainActivity.this
但是,我收到以下异常:
java.lang.IllegalStateException: Not allowed to start service Intent (service-name) : app is in background
知道这可能是什么原因吗?现在坚持了几个小时。
对于需要从前台应用调用后台应用服务的情况,我们可以按照顺序:
- 先调用 bindService()
- 由于绑定调用而在 OnServiceConnected() 之后
- 调用 StartService()。