从我的 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()
- 由于绑定(bind)调用在 OnServiceConnected() 之后
- 调用 StartService()。
关于android - startService() 抛出 java.lang.IllegalStateException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44310130/