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

半腔热情 提交于 2019-12-09 23:57:48

问题


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.

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?


回答1:


Try to use android service with START_STICKY attribute. In background thread you can listen for location changes (do not use standard LocationSensor - just implement java interface based solution).

You can also find examples of background operations for iOS.

For android you might be interested in unit 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);


来源:https://stackoverflow.com/questions/43472256/how-to-track-the-user-location-at-full-time-with-a-delphi-app-under-android-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!