错误:app在后台uid为null

ele*_*o23 11 android intellij-idea

我在IntelliJ CE 2017.2上创建了一个新的Android项目并得到了这个错误.我按照答案中的说明添加了maven部分.

当我运行使用新项目创建的默认应用程序时,我收到错误

执行时出错:am startservice com.test.myapp/com.android.tools.fd.runtime.InstantRunService

启动服务:Intent {act = android.intent.action.MAIN cat = [android.intent.category.LAUNCHER] cmp = com.test.myapp/com.android.tools.fd.runtime.InstantRunService}

错误:app在后台uid为null

我仍然可以在我的手机上手动打开应用程序并且它可以工作,但它不会自动启动,我收到错误消息.

小智 12

我使用android api 26/27(Oreo)看到了这个错误.在奥利奥,有新的背景执行限制.tl; dr你应该在前台运行服务.

要使用adb启动您的服务,请使用 adb shell am start-foreground-service

而不是adb shell am startservice.

  • 作为解决方法,您可以禁用"即时运行".转到设置 - >构建,执行,部署 - >即时运行.取消选中"在部署时启用即时运行以热插拔代码/资源更改(默认启用)" (5认同)
  • 你如何在IntelliJ中改变它?我只能设置启动标志但不能更改命令!? (3认同)

Gib*_*olt 6

奥利奥 (26+) 需要前台服务

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    context.startForegroundService(intent);
}
else {
    context.startService(intent);
}
Run Code Online (Sandbox Code Playgroud)

重要服务启动后,Service.startForeground()必须在 5 秒内调用。明显的地方是在onCreate