1

How to determine if Location service On/Off in firemonkey application?

I am aware about the article: How to check if network is available on Android ( Delphi XE5 ). I am not sure that helps me to detect if Location Service is on or off.

Community
  • 1
  • 1
Oussama Al Rifai
  • 323
  • 1
  • 6
  • 16

1 Answers1

6

To determine if the location services is On/Off, you must check if the GPS and Network Location services are activated, for this you must use the isProviderEnabled method of the LocationManager class, In Delphi this class is defined in the Androidapi.JNI.Location unit.

Check this sample

uses
  Androidapi.Helpers,
  Androidapi.JNI.Location,
  Androidapi.JNIBridge,
  FMX.Helpers.Android,
  Androidapi.JNI.GraphicsContentViewText;

{$R *.fmx}       

procedure TForm1.Button1Click(Sender: TObject);
var
  locationManager : JLocationManager;
begin
  locationManager := TJLocationManager.Wrap(
    ((SharedActivity.getSystemService(TJContext.JavaClass.LOCATION_SERVICE)) 
    as ILocalObject).GetObjectID);

  if locationManager.isProviderEnabled(TJLocationManager.JavaClass.GPS_PROVIDER) then
   ;   //do something   

  if locationManager.isProviderEnabled(TJLocationManager.JavaClass.NETWORK_PROVIDER) then 
   ;   //do something  else   

end;
BIBD
  • 15,107
  • 25
  • 85
  • 137
RRUZ
  • 134,889
  • 20
  • 356
  • 483