首页 > 解决方案 > 无法在后台启动服务

问题描述

我有一个用于监听套接字服务的服务。我只需要在应用程序处于前台时运行此服务。但是,在生产中,我看到了几次崩溃:

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);
}

我无法在本地重现此崩溃。

标签: androidandroid-serviceandroid-service-binding

解决方案


推荐阅读