简体   繁体   English

如何在android / ios下使用delphi应用程序全时跟踪用户位置

[英]How to track the user location at full time with a delphi app under android/ios

I need to track all the time the user location, even when the user is not using my app. 我需要一直跟踪用户位置,即使用户未使用我的应用程序也是如此。 However my knowledge of android/ios architecture is little low to know how to do this. 但是,我对android / ios体系结构的了解很少,不知道该怎么做。

Do I need to make my full app alive in memory at all time (and I think this will be a little waste of resources) or do I need to create a small app like service (don't even know if it's possible) to do this job? 我是否需要始终使我的完整应用程序在内存中存活(我认为这会浪费一些资源),还是需要创建一个类似服务的小型应用程序(甚至不知道是否可能)来做这个工作?

Try to use android service with START_STICKY attribute. 尝试使用具有START_STICKY属性的android服务。 In background thread you can listen for location changes (do not use standard LocationSensor - just implement java interface based solution). 在后台线程中,您可以侦听位置更改(不要使用标准的LocationSensor-仅实现基于Java接口的解决方案)。

You can also find examples of background operations for iOS. 您还可以找到iOS后台操作的示例。

For android you might be interested in unit Androidapi.JNI.Location 对于android,您可能对Androidapi.JNI.Location单元Androidapi.JNI.Location

TLocationListener = class(TJavaLocal, JLocationListener)
  public
    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

And for your service module you have to declare some variables 对于服务模块,您必须声明一些变量

TServiceModule = class(TAndroidService)
  function AndroidServiceStartCommand(const Sender: TObject;
    const Intent: JIntent; Flags, StartId: Integer): Integer;
private
  FLocationManager: JLocationManager;
  FLocationManagerService: JObject;
  FLocationListener: JLocationListener;

function TServiceModule.AndroidServiceStartCommand(const Sender: TObject;
  const Intent: JIntent; Flags, StartId: Integer): Integer;
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 := TLocationListener.Create;
      FLocationManager.requestLocationUpdates(TJLocationManager.JavaClass.GPS_PROVIDER,
        0, 0, FLocationListener, TJLooper.JavaClass.getMainLooper);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM