Planned maintenance has been done but it did not solve the problem. So work will continue on this and a new time for trying updates will be announced asap.
Keep Android Activity running, while not visible
pogojotz last edited by
we have an application, which is originally targeting desktops and we ported it to Android 8, changing only the parts really needed, so we can stay with one common code base, using
#ifdefs where needed.
My problem is now, that the application is expected to send regular packets to a server and also actively listen on a TCP connection. This works fine on a desktop. On Android this works also fine as long as the app is in foreground and display is on (or the USB charger connected). Once the app goes into doze no network interaction takes place.
I've read Sending keep-alive Messages on Android even if the device is in sleep/doze mode, where the OP seemed to have realized exactly, what I needed, by creating a foreground service and using that to keep the activity awake. I tried that and managed to create a foreground service, but just the existence of a bound foreground service does not seem to keep the activity awake and the event loop running.
Then I read somewhere that the activity is kept in foreground, if the foreground service runs in the same process, but that didn't seem to help either. (Also I had to derive my service from
android.app.Servicedirectly, because that doesn't work with
QtServiceand I don't understand the side effects).
I don't know what I missed. All I need is grant execution resources to the activity from my service every few seconds. I know I could move the network connection stuff to the service, but that would mean a huge code change and introduce inconsistencies with other platforms.
Do you have any ideas what I could be missing?
@pogojotz I think you need a background service: https://developer.android.com/guide/components/services
pogojotz last edited by
@jsulm I have tried that and this didn't work. Background services can and will be suspended as soon as there is another foreground activity/service, which seems more important to the OS. This option was also discussed in the thread I linked above. A foreground service on the other hand stays active even if the bound activity is in background.
Please also note, that I target Android 8, which imposes even stricter policies on background services, as described here.