23

I created a new Android project on IntelliJ CE 2017.2 and got this error. I added the maven section as described in the answer.

When I run the default app that is created with the new project I get the error

Error while executing: am startservice com.test.myapp/com.android.tools.fd.runtime.InstantRunService

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

Error: app is in background uid null

I can still open the app manually on my phone and it works, but it doesn't start automatically and I get that error message.

Community
  • 1
  • 1
elementzero23
  • 1,389
  • 2
  • 16
  • 38

2 Answers2

25

I've seen this error using android api 26/27 (Oreo). In Oreo there are new background execution limits. tl;dr you should run services in the foreground.

to start your service using adb, use adb shell am start-foreground-service

instead of adb shell am startservice.

CSlug
  • 269
  • 2
  • 2
  • 3
    How do you change that in IntelliJ? I can only set launch flags but not change the command!? – elementzero23 Nov 18 '17 at 08:54
  • 5
    As a workaround, you can disable Instant Run. Go to Settings -> Build, Execution, Deployment -> Instant Run. Uncheck "Enable Instant Run to hot swap code/resource changes on deploy (default enabled)" – shad Jan 12 '18 at 20:11
  • @shadanan This worked. I am not getting the error anymore and the app does start automatically. If you post this as answer I will mark it as the correct one. – elementzero23 Jan 15 '18 at 08:09
7

Oreo (26+) requires services in the foreground

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    context.startForegroundService(intent);
}
else {
    context.startService(intent);
}

IMPORTANT After the service starts, Service.startForeground() must be called within 5 seconds. The obvious place is within onCreate

Community
  • 1
  • 1
Gibolt
  • 42,564
  • 15
  • 187
  • 127