在DELPHI中使用androidservice全天候的gpsLocation

时间:2018-01-09 23:29:16

标签: android delphi background gps

尝试此代码,但我的应用程序关闭,我希望每隔一段时间获取一次GPS位置,但它现在不起作用我可视化  与udp客户端。

type
  TAndroidServiceDM = class(TAndroidService)
    function AndroidServiceStartCommand(const Sender: TObject;
      const Intent: JIntent; Flags, StartId: Integer): Integer;
  private
    { Private declarations }
    FLocationManager: JLocationManager;
    FLocationManagerService: JObject;
    FLocationListener: JLocationListener;

  protected

  public
    { Public declarations }
  end;

var
  AndroidServiceDM: TAndroidServiceDM;

implementation

{ %CLASSGROUP 'FMX.Controls.TControl' }

uses AndroidApi.JNI.App;

{$R *.dfm}

procedure send_udp(ip: string; puerto: Integer; datos: string);
var
  send_udp: TIdUDPClient;
begin
  send_udp := TIdUDPClient.Create(nil);
  send_udp.Host := ip;
  send_udp.Port := puerto;
  send_udp.Send(datos);
  send_udp.Free;
end;

function TAndroidServiceDM.AndroidServiceStartCommand(const Sender: TObject;
  const Intent: JIntent; Flags, StartId: Integer): Integer;
  var
  iter:JIterator;
  location : JLocation;
begin

  Result := TJService.JavaClass.START_STICKY;
  FLocationManagerService := TAndroidHelper.Context.getSystemService(
    TJContext.JavaClass.LOCATION_SERVICE);
  FLocationManager := TJLocationManager.Wrap(
    (FLocationManagerService as ILocalObject).GetObjectID);

  if FLocationManager.isProviderEnabled(
    TJLocationManager.JavaClass.GPS_PROVIDER) then
  begin
    FLocationListener := TJLocationListener.Create;
      FLocationManager.requestLocationUpdates(TJLocationManager.JavaClass.GPS_PROVIDER,
        0, 0, FLocationListener, TJLooper.JavaClass.getMainLooper);


  end;
  location := FLocationManager.getLastKnownLocation(TJLocationManager.JavaClass.GPS_PROVIDER);

   send_udp('127.0.0.1',5555,'Servicio Iniciado'+location.getLatitude.ToString);

end;

end.

如果有人可以帮助我使用此代码或其他人发送gps位置

我需要每10分钟发一次。

1 个答案:

答案 0 :(得分:0)

代码中缺少的1部分是您的FLocationListener。你需要实现它!完成后,当手机移动时,onLocationChanged功能将被呼叫(退回)。另一部分是指定最小报告频率和最小时间和最小距离(我的代码中为10秒和30米)。 即使在服务中也不需要定时器或循环。

以下是我的Delphi Android Service项目中的一些代码(只是一些重要部分)我希望您能将它们正确地放在项目中,因为我不想将整个项目粘贴到这里! / p>

  TLocationListener = class;

...

  locationListener: TLocationListener;

...

TLocationListener = class(TJavaLocal, JLocationListener)
private
  [weak] FParent : TMainService;
public
  constructor Create(AParent : TMainService);
  procedure onLocationChanged(location: JLocation); cdecl;
  procedure onProviderDisabled(provider: JString); cdecl;
  procedure onProviderEnabled(provider: JString); cdecl;
  procedure onStatusChanged(provider: JString; status: Integer; extras: JBundle); cdecl;
end;

...

procedure TMainService.StartAndroidGPS;
var
  LocationManagerService: JObject;
  location: JLocation;
begin

  if not Assigned(FLocationManager) then
  begin
    LocationManagerService :=
      TAndroidHelper.Context.getSystemService(TJContext.JavaClass.LOCATION_SERVICE);
    FLocationManager := TJLocationManager.Wrap(
      (LocationManagerService as ILocalObject).GetObjectID
      );
    if not Assigned(locationListener) then
      locationListener := TLocationListener.Create(self);
  end;

   FLocationManager.requestLocationUpdates(TJLocationManager.JavaClass.GPS_PROVIDER,
          Round(10 * 1000), 30, locationListener,
            TJLooper.JavaClass.getMainLooper);

  // just to get started, ask the GPS for the Last Known Position (if any)
  location := FLocationManager.getLastKnownLocation(TJLocationManager.JavaClass.GPS_PROVIDER);
  LocationisChanged(location);

end;      

...

procedure TMainService.StopAndroidGPS;
begin
  if Assigned(locationListener) then
    FLocationManager.removeUpdates(locationListener);
end;

...

{ TLocationListener }

constructor TLocationListener.Create(AParent: TMainService);
begin
  inherited Create;
  FParent := AParent;
end;

procedure TLocationListener.onLocationChanged(location: JLocation);
begin
  FParent.LocationisChanged(location);
end;

...

procedure TMainService.LocationisChanged(location: JLocation);
var
  lc: TLocationCoord2D;
begin
  // do whatever you need to do with the location here, for example:
  lc := TLocationCoord2D.Create(location.getLatitude, location.getLongitude))
end;