从我的Activity的onCreate()
方法开始,
我试图通过以下代码启动一个服务:
Intent intent = new Intent();
intent.setClassName(SERVICE_PKG_NAME, SERVICE_NAME);
context.startService(intent); //context = MainActivity.this
然而,我收到了以下异常:
java.lang.IllegalStateException: 不允许启动服务意图(服务名称):应用在后台运行
有什么想法是什么原因导致这种情况?我已经被卡住几个小时了。
startService()
。 - CommonsWareContext
上调用startForegroundService()
而不是startService()
。 - CommonsWare