How to keep service running in background in Android O?
Asked Answered
S

1

7

I need to keep a service running in background. This service is used to keep a Socket.IO/MQTT connection. In versions previous to Android O there were no problem keeping the service alive, but in Android O this is not possible because of the background execution limits.

Is there any way to keep the services running, besides foreground service?

Spin answered 6/7, 2018 at 21:46 Comment(7)
Nope. You'd need a persistent notification as a foreground service to keep it consistently running.Zsigmondy
Background execution limit was made especially to counter heavy battery drain of multiple sockets. Otherwise resort to periodic jobs.Ironmonger
Your service can run in the background for 5 seconds before you need to attach a notification to it. You might wanna read all about Android O background limitatonsFormal
are you solve this issue? @Mariano CordobaBotheration
@RevathiManoharan no. I'm using a foreground service for persistent connections and workmanager for tasks that are executed at fixed rates.Golly
Ok let try jobIntent too.but my redmi note pro device working with when app fully closed i tried to push notify then my app got wokeup..It's achieved without using job intent here i used only startForegroundService.but same scenario not working in One plus device.i thought its devices specific problem.so u have to try someother android devices tooBotheration
In redmi note 5 pro while app is in background when I tried push notification its workingBotheration
A
0

You should use Firebase Cloud Messaging to push data from a server, this is to prevent all apps opening their own connection and staying active, draining the battery and filling up the RAM for no value to the user. Your app should be no exception to that.

For short lived connections (minutes), or user sessions (minutes, too, unless the user stays active longer), a foreground service might be a solution.

Out of curiosity, what is your use case for wanting a "persistent connection"?

Actino answered 30/3, 2019 at 16:9 Comment(5)
I need to keep track of the presence of the user. I don't think FCM would help in this case, probably the best choice is to use a foreground service.Golly
What do you consider "presence of the user"?Actino
Also, do you really need it to be sent over the internet in real-time (I mean seconds, which still depends on network availability)?Actino
By presence i mean if the user is online or not. And yes, i need it to be near-real-time.Golly
That doesn't answer my first question. I can rephrase it this way: When do you consider the user is no longer online, and when do you consider he is back online?Actino

© 2022 - 2024 — McMap. All rights reserved.