如何从 FireMonkey 中的 GPS 传感器获取 Android 的当前日期和时间?

How can I get the current date and time from the GPS sensor in FireMonkey for Android?

如何使用 Delphi Android 编程从移动设备上的 GPS/glonas/baido 颗卫星获取当前日期时间?

此时我只能从位置传感器获取经度和纬度。

procedure TForm1.LocationSensor1LocationChanged(Sender: TObject;
  const OldLocation, NewLocation: TLocationCoord2D);
var
  LDecSeparator: String;
begin
  LDecSeparator := FormatSettings.DecimalSeparator;
  FormatSettings.DecimalSeparator := '.';
  // Show current location
  ListBoxItemLatitude.ItemData.Detail  := Format('%2.6f', [NewLocation.Latitude]);
  ListBoxItemLongitude.ItemData.Detail := Format('%2.6f', [NewLocation.Longitude]);
end;

我知道使用 GSM 网络更方便,但我的情况下没有可用的移动网络。

这可能吗?

当您从提供的框架连接位置传感器时,它只会为您提供位置数据

但你可以获得原始数据和抓取时间数据

仅供参考

因为手机公司在他们的产品上使用了 chap gps 硬件,所以输出数据的准确性较低

例如数据以 1Hz 的速率采样,因此您获得的最佳值是准确度 1 秒:

使用此代码:

    procedure TForm1.Button1Click(Sender: TObject);
    var
      LocationManagerService: JObject;
      location : JLocation;
    begin
      if not Assigned(FLocationManager) then
      begin
        LocationManagerService := SharedActivityContext.getSystemService(TJContext.JavaClass.LOCATION_SERVICE);
        FLocationManager := TJLocationManager.Wrap((LocationManagerService as ILocalObject).GetObjectID);
        if not Assigned(locationListener) then
          locationListener := TLocationListener.Create(self);
        FLocationManager.requestLocationUpdates(TJLocationManager.JavaClass.GPS_PROVIDER, 10000, 10, locationListener,
            TJLooper.JavaClass.getMainLooper);
      end;                      

      location := FLocationManager.getLastKnownLocation(TJLocationManager.JavaClass.GPS_PROVIDER);
  Memo1.Lines.Add(location.getTime.ToString);
    end;

此输出类似于 1505216957000 时间戳,但由于精度较低,最后 3 位数字始终为 0,因此您必须从数字的最后删除 000,然后将其转换为您的时区

这不是 GPS 网络时间。它是 GPS 接收器的最后一个有效位置 FIX,可能是在一段时间之前。