5

I have an interesting issue with TLocationSensor. If the location is allowed in the Android menu before I start my application, the sensor works fine. I catch the event OnLocationChange. But if I don't allow location in the Android menu and start the application and then allow location in the Android menu the sensor doesn't work.

Event OnLocationChange is not called.

Set LocationSensor1.Active := true; doesn't help in this case.

How can I automatically allow location after my application starts?

I am using Delphi XE10.1 Berlin and Android 6.0

Akshay Anurag
  • 724
  • 1
  • 8
  • 27
milenjao
  • 93
  • 5
  • 18
  • start the LocationSensor1 from a timer event and disable the timer after. And enable the Timer in the FormCreate event – ColdZer0 Oct 02 '16 at 03:44
  • This is peculiar. I tried this out myself. I started the app, which has the location sensor set `Active` at design time, with the location settings disabled on the device. Then when the app was running I enabled the location settings. Sure enough, as expected the `onLocationChanged` method in the `JLocationListener` implementation in System.Android.Sensor.pas called `TCustomLocationSensor.DoLocationChanged`, which in turn called my location sensor's `OnLocationChanged` event handler. I wonder what is different in your case. You have tried a trivial self-contained example, like I have done? – blong Oct 02 '16 at 20:39
  • 1
    I make few tests. Sometimes sensor start works but in many cases not. I dont understand this behavior. Chance to sensor start work is higger if I allow Location immediately after application starts. Location sensor is still set as Active ! – milenjao Oct 03 '16 at 07:33
  • 2
    I found this same problem when running the Delphi sample OrientationSensor on Android. Only one 0.00 / NaN sensor reading is received. In order to deal with API level 26 permissions, you cannot set active := true at design time, so there is no way to start the sensor – Omar Reis Nov 06 '18 at 14:54
  • What do u mean by no way to start the sensor? – Dreamer64 Oct 04 '19 at 21:10

2 Answers2

0

Battled the same issue today. Manually setting the app permissions did the job in my case

Edit: Manually added to AndroidManifest.xml:

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
Radacina
  • 431
  • 5
  • 10
0

With recent android versions it is necessary to request the permissions during runtime (on start or maybe better if and when you need it).

{$IFDEF ANDROID}
uses
  System.Permissions,
  Androidapi.JNI.OS; 

PermissionsService.RequestPermissions([
  JStringToString(TJManifest_permission.JavaClass.ACCESS_COARSE_LOCATION),
  JStringToString(TJManifest_permission.JavaClass.ACCESS_FINE_LOCATION)],
  procedure(const APermissions: TArray<string>; const AGrantResults: TArray<TPermissionStatus>)
  begin
    if (Length(AGrantResults) <> 2)
      or (AGrantResults[0] <> TPermissionStatus.Granted)
      or (AGrantResults[1] <> TPermissionStatus.Granted) then 
      Log('Location permissions not granted');
  end);
{$ENDIF}

Of course, both permissions also have to be checked in project options, but this is already the default.

maf-soft
  • 2,335
  • 3
  • 26
  • 49