ios LocationSensor 后台运行

设置好,LocationSensor.active := true。

当按home键或黑屏后,程序将立马挂起。LocationSensorLocationChanged事件也不会响应。

有人说要两三秒才挂起,到我这却是立马,不知为啥。

在网上知道的解决方案:http://www.fmxexpress.com/learn-to-use-the-location-sensor-in-the-background-with-firemonkey-in-delphi-10-berlin-on-ios/

经测试后,发现它并不能完全做到后台一直运行。一般运行十来分钟就停止了。

具体解决方案:在上述网址基础(FLocater.setAllowsBackgroundLocationUpdates(True);)上,还需要加上

FLocater.setPausesLocationUpdatesAutomatically(False);

也就是

if TOSVersion.Check(9) and (FLocater <> nil) then

  begin
    {$IF Defined(BACKGROUNDUPDATES) and Defined(CPUARM64)} // for some reason, this function crashes in 32-bit
    FLocater.requestAlwaysAuthorization;
    FLocater.setAllowsBackgroundLocationUpdates(True);
    FLocater.setPausesLocationUpdatesAutomatically(False);
    {$ENDIF}
  end;

 

posted @ 2017-07-11 12:22  竹鹿风清  阅读(359)  评论(0编辑  收藏  举报