GPS turned off when app in background

Hello,

I am writing an app that has to read the GPS even in background.

I managed to maintain the app working in background (what in itself is an issue in ai2), thanks to Ullis Roboter's wonderful tools:
https://ullisroboterseite.de/android-AI2-KeepAlive-en.html
(Thanks!)
I am sure the app is running in bg as, for example, it produces sounds from time to time, items are updated, etc.

But... the GPS feature stops working after some time in bg. I mean, everything in the app goes on working but the "ChangeLocation" event of the location sensor is not fired, so that the coordinates read by the app are "frozen". Until the app goes to foreground again... then GPS starts again.

I turned OFF "battery optimization" for the app in Android's setting but it didn't solve the problem.

Any suggestion?

Many thanks in advance!

Cheers,

Maybe this will help ?

https://puravidaapps.com/locationservice.php

1 Like

This is a possible explanation if you are testing with Android 8+:

In an effort to reduce power consumption, Android 8.0 (API level 26) limits how frequently an app can retrieve the user's current location while the app is running in the background. Under these conditions, apps can receive location updates only a few times each hour.

What you experience is probably expected behavior.

How often are you updating the device's location?
You said "the GPS feature stops working after some time in bg"; how long is 'some time'?

Keep the app in the foreground? :cry: Use a StayAwake routine. Here is how you might code a Stay Awake block routine using a Notifier

or you could use Taifun’s Tools Extension: https://puravidaapps.com/tools.php

Using either an extension or a block routine drains your device battery rapidly . These work around methods are not running the app in the Background, they force the app
screen to stay open .

Thanks Steve.

I think it stops reading GPS as soon as it goes to bg, or shortly after.

this is why I wrote a locationservice extension...
https://puravidaapps.com/locationservice.php

Location Service Extension
This extension is able to run in the background while your app is closed and stores location data (latitude, longitude and optionally current datetime, altitude, accuracy, speed, current address and provider) in TinyDB.
Also a background web functionality is available
which can be used to send the location data to a web service of your choice using a POST request. This could be used for example to store the location data in a MySQL database or to send an email after a location change was detected while the app is not running.
A notifier will be displayed while the location service is running in the background.

Taifun

Thanks Taifun.

Your extension looks nice. Do you think it will work on Android >=8.0 taking into account what Steve said?

This is a possible explanation if you are testing with Android 8+: In an effort to reduce power consumption, Android 8.0 (API level 26) limits how frequently an app can retrieve the user's current location while the app is running in the background. Under these conditions, apps can receive location updates only a few times each hour .

Yes

Tested successfully on Samsung Galaxy A5 running Android 8 and Galaxy A51 running Android 10. I also tested on the following emulators: Nexus 5 running Andrid 5, Nexus 6 running Android 6, Nexus 5X running Android 8 and Xiaomi Redmi Note 7 running Android 9.

Taifun

@Sito_P

If you use Taifun's excellent extension, the coordinates read by the app in real time are "frozen" and stored in a TinyDB until the app returns to foreground again . The extension then can report the locations captured when asleep when it is awakened. The location changes are stored but not reported in the app until the app returns to the foreground. As far as I can tell, you do not receive continuous notifications on the device while the app is in the background capturing location data. The coordinates are captured but you read them in arrears, not real time. You can reproduce a trak showing where you have been while the app hibernated, which might be all you want to do anyway. :slight_smile:

No. I'd need to read the locations in background in "real time".

@Taifun, can you confirm this? Is your extension limited to store the locations read in bg in TinyDB? I guess there is no special reason for that... If you can access the GPS in bg you could in principle fire any app function with them, besides storing in TinyDB (?).

If your extension can also send these locations to a given server in real time, what seems to be the case, maybe I could work with that. Although the in-app solution would be much better.

Thanks guys for your help.

D: if you think your problem fixed but you have dont money