android - 无法在后台启动服务
问题描述
我有一个用于监听套接字服务的服务。我只需要在应用程序处于前台时运行此服务。但是,在生产中,我看到了几次崩溃:
IllegalStateException
Not allowed to start service Intent { cmp=com.app.app/.app.sockets.EventHandlingService }: app is in background
最初,我在我的 Activity 的 onCreate 中调用它,并且根据其他一些 SO 问题,从技术上讲,该应用程序当时并没有被前景化。所以我把它移到了 onResume 中,但我仍然看到它正在发生。它是这样开始的:
@Override
protected void onResume() {
super.onResume();
Intent serviceIntent = new Intent(this, EventHandlingService.class);
startService(serviceIntent);
bindService(serviceIntent, serviceConnection, 0);
}
我无法在本地重现此崩溃。
解决方案
推荐阅读
- python - Python, Telethon 发送相册
- swift - NavigationController PushedView 控制器关闭整个 NavigationController - swift - 以编程方式
- javascript - 查找字符串是否包含数组的任何值的更快方法
- reactjs - history.push 不起作用反应路由器 dom
- spring-boot - 如何在spring-boot中根据路径变量应用不同的验证规则
- r - R:使用j中的匹配在两个data.tables之间移动数据
- javascript - componentWillReceiveProps/getDerivedStateFromProps 的用途
- python - 使用pyzbar读取python中的模糊条码
- java - 创建一个改变耳机音量的方法,java类
- php - 解决“致命错误:在 null 上调用成员函数 children()”