I have an Android app written in Firemonkey (Delphi 11.1) that requires the GPS accuracy to be logged as well. I managed to get the GPS information from Android using the AndroidAPI.JNI JLocationManager,and it seems to be working as expected. Latitude, longitude, accuracy and altitude is now available.
The problem is that the app runs fine when my phone is connected to a power supply and charging, but if not charging, then the app crashes shortly after updating the location. There is no error message, and I can't trap the error in the code. It simply closes the app, and then Android states that the app keeps on crashing. My best guess is that it has to do with the battery optimisation settings, but I have tried changing it on the phone but makes no difference. Can someone recommend something else I can try? Is there a log on the phone itself where I can look for the potential reason of this crash? I tried looking for something unders Settings->Developer options, but I can't find a log there, only settings to log.
I'm using the following code:
procedure TLocationObj.LoadProviders;
var
LocationManagerService: JObject;
iter : JIterator;
location : JLocation;
i: Integer;
begin
try
if not Assigned(LocationManager) then
begin
LocationManagerService := SharedActivityContext.getSystemService(TJContext.JavaClass.LOCATION_SERVICE);
LocationManager := TJLocationManager.Wrap((LocationManagerService as ILocalObject).GetObjectID);
if not Assigned(locationListener) then
locationListener := TLocationListener.Create(self);
LocationManager.requestLocationUpdates(TJLocationManager.JavaClass.GPS_PROVIDER, 10000, 100, locationListener,
TJLooper.JavaClass.getMainLooper);
end;
location := LocationManager.getLastKnownLocation(TJLocationManager.JavaClass.GPS_PROVIDER);
onLocationChanged(location);
except
on E : Exception do
begin
ShowMessage('Exception class name = '+E.ClassName);
ShowMessage('Exception message = '+E.Message);
end;
end;
end;
I've tried changing the MinTime and MinDistance parameters on the requestLocationUpdates command but it made no difference.