Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: app is in background uid null

Tags:

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.

like image 824
elementzero23 Avatar asked Nov 09 '17 19:11

elementzero23


Video Answer


2 Answers

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.

like image 74
CSlug Avatar answered Sep 20 '22 08:09

CSlug


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

like image 40
Gibolt Avatar answered Sep 19 '22 08:09

Gibolt