Home gpsLocation with androidservice in fulltime in DELPHI
Reply: 1

gpsLocation with androidservice in fulltime in DELPHI

Jhxnnxthxn kpl
1#
Jhxnnxthxn kpl Published in 2018-01-09 23:29:15Z

Try this code, but my application closes, I want to get the GPS location every so often but it does not work for now I'm visualizing with udp client.

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.

if someone can help me with this code or some other to send the gps location

I need to send the position every 10 minutes.

nolaspeaker
2#
nolaspeaker Reply to 2018-01-10 18:38:01Z

The 1 part that's missing from the code is your FLocationListener. You need to implement that! Once you do, the onLocationChanged function will be call(ed back) when the phone moves. The other part is specifying the minimum reporting frequency and the minimum time and minimum distance (10 seconds and 30 meters in my code). There is no need for timers or loops, not even in a Service.

Here is some of my code from my Delphi Android Service project (just some of the important parts) I hope you can place them correctly in your project because I don't want to paste my whole project in here!

  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;
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.305149 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO