Delphi xe10 android service gps

I am using Emabarcadero Delphi XE 10Seattle Update 1 to create an Android service, I found this tutorial Linkand created an application application, what I want to do is get the current location every 5 seconds using a location sensor and save it to the database. I do this on my main form, but when I close the program, it stops, so I can not find an article on how I can synchronize my application and service. I tried to add a timer and Locationsensor to the service form, but Delphi does not allow me to add any object to the service form, I think I need to send a service command or somehow I need to connect to the service and program, I found a sample that shows how upload images using the service, but this is also an expert for beginners like me :)

+4
source share
2 answers

Check out this article . Daniele Spinetti solves the problem using the LocationSensor service.

Place the System.Sensors and System.Android.Sensors systems in your project and make changes (Delphi 10 Seattle).

System.Sensors.pas

// about line 748
implementation
uses
  System.Variants, System.Math, System.Character,
{$IFDEF ANDROID}
  // -- patch 
  // include the modified System.Android.Sensors
  System.Android.Sensors;
{$ENDIF ANDROID}

System.Android.Sensors.pas

//about line 12
uses
  // -- patch 
  // use the modified System.Sensors
  System.Sensors;
...
// about line 70
class constructor TPermission.Create;
var
  PackageInfo: JPackageInfo;
  PackageManager: JPackageManager;
  Activity: JActivity;
  LContext: JContext;
begin
  // -- patch 
  // Activity := TJNativeActivity.Wrap
  // (PANativeActivity(System.DelphiActivity)^.clazz)
  LContext := TJContextWrapper.Wrap(System.JavaContext);
  PackageManager := LContext.getPackageManager();
  PackageInfo := PackageManager.getPackageInfo
    (LContext.getApplicationContext.getPackageName,
    TJPackageManager.JavaClass.GET_PERMISSIONS);
  FPermissions := PackageInfo.requestedPermissions;
end;
...
// about line 100
type
  TAndroidGeocoder = class(TGeocoder)
  private type
    TGeocoderRunnable = class(TJavaLocal, JRunnable)
    private
      FCoord: TLocationCoord2D;
      FLGeocoder: JGeocoder;
    public
      constructor Create(ACoord: TLocationCoord2D; AGeocoder: JGeocoder);
      procedure run; cdecl;
    end;
  private
  class var
    FGeocoder: JGeocoder;
    // FActivity: JActivity; // -- patch 
    FActivity: JContextWrapper; // -- patch 
...
// about line 130
  TUIAndroidLocationSensor = class(TCustomLocationSensor)
  private
    FPermitted: Boolean;
    // FActivity: JNativeActivity; // -- patch 
    FActivity: JContext; // -- patch 
    FLastValue: JLocation;
    FLocationManager: JLocationManager;
    FAccuracy: TLocationAccuracy;
...
// about line 1500
constructor TUIAndroidLocationSensor.Create(AManager: TSensorManager);
var
  LocationService: JObject;
begin
  inherited;
  // FActivity := TJNativeActivity.Wrap
  // (PANativeActivity(System.DelphiActivity)^.clazz); // -- patch 
  FActivity := TJContext.Wrap(System.JavaContext); // -- patch 
  LocationService := FActivity.getSystemService
    (TJContext.JavaClass.LOCATION_SERVICE);
  if Assigned(LocationService) then
    FLocationManager := TJLocationManager.Wrap((LocationService as ILocalObject)
      .GetObjectID);
end;
...
// about line 1530
  function RunIfPossible(var ARunnable: TLocationRunnable;
    var AListener: TLocationListener; AProviderName: JString): Boolean;
  var
    Provider: JLocationProvider;
    LHandler: JHandler;
  begin
    Result := False;
    if FLocationManager.isProviderEnabled(AProviderName) then
    begin
      if AListener = nil then
        AListener := TLocationListener.Create(Self);
      Provider := FLocationManager.getProvider(AProviderName);
      if Provider <> nil then
      begin
        ARunnable := TLocationRunnable.Create(FLocationManager, AListener,
          AProviderName);
        // FActivity.runOnUiThread(ARunnable); // --patch 
        // -- patch 
        // You can use post method of Handler instead runOnUiThread in this case.
        // more info here: http://developer.android.com/guide/topics/fundamentals/processes-and-threads.html
        LHandler := TJHandler.JavaClass.init;
        LHandler.post(ARunnable);
        Result := True;
      end;
    end;
  end;
...
// about line 1730
class constructor TAndroidGeocoder.Create;
begin
  // -- patch 
  // FActivity := TJNativeActivity.Wrap
  // (PANativeActivity(System.DelphiActivity)^.clazz);
  FActivity := TJContextWrapper.Wrap(System.JavaContext);
  FGeocoder := TJGeocoder.JavaClass.init(FActivity);
end;

Then you can use LocationSensor as follows:

uses System.Sensors, System.Android.Sensors;
...
var
  FSensors: TSensorArray;
  Sensor: TCustomSensor;
begin
  TSensorManager.Current.Active := true;
  FSensors := TSensorManager.Current.GetSensorsByCategory(TSensorCategory.Location);

  FSensor := nil;
  for Sensor in FSensors do
  begin
    if TCustomLocationSensor(Sensor).SensorType = TLocationSensorType.GPS then
    begin
      FSensor := TCustomLocationSensor(Sensor);
      Break;
    end;
  end;

  if not Assigned(FSensor) then
    Exit; { no location sensor is available }

  { start the sensor if it is not started }
  if not FSensor.Started then
    FSensor.Start;
+1
source

if TTimer doesn't work as you expected, try to do all your stuff in an anonymous thread and use TThread.CurrentThread.Sleep in the main thread

function TServiceModule.AndroidServiceStartCommand (const Sender: TObject;
  const Intent: JIntent; Flags, StartId: Integer): Integer;
begin
  Result: = TJService.JavaClass.START_STICKY;

  TThread.CreateAnonymousThread (procedure
    begin
      repeat
        TThread.Current.Sleep(MSecsPerSec);
        TThread.CreateAnonymousThread(procedure
          DoStuff();
        end).Start;
        TThread.Current.Sleep(MSecsPerSec);
      until (TThread.Current.CheckTerminated);
    end).Start;
end;
+1

Source: https://habr.com/ru/post/1618875/


All Articles