首页 > 解决方案 > 为什么“后台位置更新”只有在应用重启后才开始工作?

问题描述

  1. 运行应用程序
  2. 切换到背景。
  3. “后台位置更新”在后台启动,但不到一分钟。
  4. 我单击应用程序图标或返回应用程序,“位置更新”开始工作
  5. 回到背景。“背景位置更新”现在可以长期使用。

问题是什么?为什么第一次过渡到后台模式“后台位置更新”会在不到 30 秒内停止工作。

public LocationManager()
        {
           this.locMgr = new CLLocationManager
           {
               PausesLocationUpdatesAutomatically = false
           };
           locMgr.AllowsBackgroundLocationUpdates = true;
           locMgr.DesiredAccuracy = 1;
           locMgr.DistanceFilter = 1;
           locMgr.StartUpdatingLocation();
}

我的 info.plist

<key>UIBackgroundModes</key>
<array>
    <string>location</string>
</array>
<key>NSLocationAlwaysUsageDescription</key>
    <string>This application needs access to a location.</string>
    <key>NSLocationWhenInUseUsageDescription</key>
    <string>This application needs access to a location.</string>
    <key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
    <string>This application needs access to a location.</string>

我的 AppDelegate.cs

private CLLocationManager locationManager = new CLLocationManager();
public override void DidEnterBackground(UIApplication application)
        {
            Manager = new LocationManager();
            Manager.StartLocationUpdates();
        }

标签: c#iosxamarin.forms

解决方案


推荐阅读