Oussama Al Rifai Oussama Al Rifai - 3 months ago 26
Android Question

Determine if Location Service is on

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.

Answer

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;
Comments