【问题标题】:How to track the user location at full time with a delphi app under android/ios如何使用android/ios下的delphi应用全程跟踪用户位置
【发布时间】:2017-04-18 12:32:02
【问题描述】:

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

我是否需要让我的整个应用程序一直在内存中存活(我认为这会有点浪费资源)或者我是否需要创建一个类似服务的小应用程序(甚至不知道是否有可能) 来完成这项工作?

【问题讨论】:

标签: android ios delphi firemonkey


【解决方案1】:

尝试使用带有 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);

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-22
  • 1970-01-01
  • 2016-11-14
  • 2016-10-01
相关资源
最近更新 更多