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

即使用户没有使用我的应用程序,我也需要跟踪用户所在的位置。 然而,我对android / ios架构的了解并不知道如何做到这一点。

我是否需要随时在内存中使用我的完整应用程序(我认为这会浪费一些资源)或者我是否需要创建一个小应用程序,如服务(甚至不知道是否可能)这个工作?

尝试使用具有START_STICKY属性的android服务。 在后台线程中,您可以监听位置更改(不要使用标准的LocationSensor – 只需实现基于java接口的解决方案)。

您还可以找到iOS的后台操作示例。

对于android你可能感兴趣的是单元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 

对于您的服务模块,您必须声明一些变量

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