startService()抛出java.lang.IllegalStateException [英] startService() throws java.lang.IllegalStateException

查看:196
本文介绍了startService()抛出java.lang.IllegalStateException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从我的活动的onCreate()

我正在尝试通过以下代码启动服务:

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:不允许启动服务意图(服务名称):应用程序在后台

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.

推荐答案

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

For cases involving need to invoke service of Background app from a foreground app, We can follow the sequence:

  • 首先调用bindService()
  • 由于绑定调用而在OnServiceConnected()之后
  • 致电StartService().

这篇关于startService()抛出java.lang.IllegalStateException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆