Android Question GPS reading, after a few hours it turns off - how to understand the reason for the shutdown?

amorosik

Well-Known Member
Licensed User
I have a program that, using as a basis the gps reading indicated by Erel, records the gps position every 60 seconds on a local log file
After several hours of proper operation, and only on some phones (same phone brand/model, on all Android 10, on all same sim/telephone operator) , the app suddenly shuts down
I read on some articles that it could be the intervention of the s.o. which detects programs that are consuming too many resources and brutally shuts down without asking anyone's
permission
So the question is: how to (if possible), from app code, to understand if the closing command is given by the operating system?
If it is not possible to understand from the app code who is killing me, is there the possibility of monitoring from a third party program which apps have been killed by the s.o.?
 

amorosik

Well-Known Member
Licensed User
Haven't tried it yet
Thanks for the suggestion, I'll do it
But first I would like to understand if that was the reason, and then if it was the operating system that shut down or not
Given that the app functionality is correct for several hours, it would seem unlikely that the operating system would intervene, if so it would have had to intervene before
 
Upvote 0

sfsameer

Expert
Licensed User
Longtime User
But first I would like to understand if that was the reason, and then if it was the operating system that shut down or not

The GPS has to be in a background service, because :
1- Android will either stop or intercept the apps that use too much memory
2- Android has a feature called battery optimization which will prevent your app to work properly if you use these type of functions in the UI and not in the background service

We have built a tracking system for a very large company, and after a while they started reporting this problem.
using the background service solved this issue for us.

a tip :
use lower gps interval example : 5-10 ms to get better results.
 
Upvote 0

amorosik

Well-Known Member
Licensed User
The GPS has to be in a background service, because :
1- Android will either stop or intercept the apps that use too much memory
2- Android has a feature called battery optimization which will prevent your app to work properly if you use these type of functions in the UI and not in the background service

We have built a tracking system for a very large company, and after a while they started reporting this problem.
using the background service solved this issue for us.

a tip :
use lower gps interval example : 5-10 ms to get better results.

The app is always in the foreground, with the display always active, the phone is normally always connected to a power cable
Gps code (initialization, start, stop, reading,..) is under service module section

B4X:
Sub Process_Globals
    Public GPS1 As GPS
    End Sub
    
Sub Service_Create
    GPS1.Initialize("GPS")
    StartGps
    End Sub
    
Public Sub StartGps
    Try
        GPS1.Start(0, 0)
        Main.gps_on=1
        Catch
        Main.gps_on=0
        End Try
    End Sub   
    
Sub GPS_LocationChanged (Location1 As Location)
    Try
        Private pagGps As frmGps
        pagGps = B4XPages.GetPage("gps")
        CallSub2(pagGps, "LocationChanged", Location1)
        Catch
        Log(LastException)
        End Try
    End Sub

What do you mean 'that use too much memory'?
How can I tell if my app is using 'too much memory'?
And how can i make sure if android shuts down my app for this reason?
The 'battery optimization' feature has been expressly disabled on the app
What do you mean by 'use lower gps interval' ?
Currently with the vehicle in motion, the GPS returns a position approximately every second
Of all these points we only use one point every 60 seconds
How do you get a reading every 5ms (200 readings / sec)?
 
Upvote 0

sfsameer

Expert
Licensed User
Longtime User
The app is always in the foreground, with the display always active, the phone is normally always connected to a power cable
Gps code (initialization, start, stop, reading,..) is under service module section

B4X:
Sub Process_Globals
    Public GPS1 As GPS
    End Sub
   
Sub Service_Create
    GPS1.Initialize("GPS")
    StartGps
    End Sub
   
Public Sub StartGps
    Try
        GPS1.Start(0, 0)
        Main.gps_on=1
        Catch
        Main.gps_on=0
        End Try
    End Sub  
   
Sub GPS_LocationChanged (Location1 As Location)
    Try
        Private pagGps As frmGps
        pagGps = B4XPages.GetPage("gps")
        CallSub2(pagGps, "LocationChanged", Location1)
        Catch
        Log(LastException)
        End Try
    End Sub

What do you mean 'that use too much memory'?
How can I tell if my app is using 'too much memory'?
And how can i make sure if android shuts down my app for this reason?
The 'battery optimization' feature has been expressly disabled on the app
What do you mean by 'use lower gps interval' ?
Currently with the vehicle in motion, the GPS returns a position approximately every second
Of all these points we only use one point every 60 seconds
How do you get a reading every 5ms (200 readings / sec)?
Wow too many questions.

1- To set the interval :
1615537321568.png

The minimum Time is the interval for each read.
to get 5 ms reading : gps.start(5,0)

*Reading your code, you are not getting the read every 60 seconds, you are getting the gps upon "GPS_LocationChanged" unless you are turning it off and on in a timer which is very wrong.


2- Too much memory means you are loading UI with timers and other functions/animations and maybe you are using google maps as well and on top of all of that you are requesting GPS reading.
so using GPS in a background service is the best solution.

Just don't over think this:
1-put your GPS in a background service.
2-Test it outside your office/home because "GPS_LocationChanged" will fire unless you use it outside.

And your app will work without any problems.
 
Upvote 0

amorosik

Well-Known Member
Licensed User
Thank you very much for the advice
On my code the instruction GPS1.Start (0, 0) is used which should make the gps reading work at the maximum possible speed
The GPS_LocationChanged routine is started about every second, when the vehicle is moving at about 50 Km/h, from this I assumed that the maximum speed of gps peripheral reading is about 1 reading / sec
So what does it mean to read the gps device every 5ms?
 
Upvote 0

sfsameer

Expert
Licensed User
Longtime User
So what does it mean to read the gps device every 5ms?
it means that every 5 ms the gps will get the location.
even if the device is moving, it will get the gps every 5ms.

For me i would recommend that you set it to 5ms or 10 ms because it will give the app more time to process the request.
it may seem too little (5ms) but it makes a big difference when you run your GPS tracking for hours.
 
Upvote 0

Omar Parra A.

Expert
Licensed User
Longtime User
Solution and see #Post


B4A and B4i
 
Last edited:
Upvote 0
Top