Xamarin iOS-CLLocationManager在后台以设置的时间间隔获取位置

Xamarin iOS-CLLocationManager在后台以设置的时间间隔获取位置,ios,background,xamarin,geolocation,location,Ios,Background,Xamarin,Geolocation,Location,我正在尝试向我的应用程序添加一个功能,该功能需要在设置的时间间隔内获取用户的当前位置。问题是,每当我使用System.Threading.Timer时,创建一个新的System.Threading.Thread并使用Thread.Sleep(),或者使用NSTimer.CreateScheduledTimer应用程序将在后台停止运行CLLocationManager LocationManager.cs public class LocationManager { protected C

我正在尝试向我的应用程序添加一个功能,该功能需要在设置的时间间隔内获取用户的当前位置。问题是,每当我使用
System.Threading.Timer
时,创建一个新的
System.Threading.Thread
并使用
Thread.Sleep()
,或者使用
NSTimer.CreateScheduledTimer
应用程序将在后台停止运行
CLLocationManager

LocationManager.cs

public class LocationManager
{
    protected CLLocationManager locationManager;
    /// <summary>
    /// Time in milliseconds between location checking.
    /// </summary>
    private int serviceMillisecondTimeout = 10000;

    public LocationManager()
    {
        this.locationManager = new CLLocationManager();

        // iOS 8 additional permissions requirements
        if (UIDevice.CurrentDevice.CheckSystemVersion(8, 0))
        {
            locationManager.RequestAlwaysAuthorization();
        }

        locationManager.LocationsUpdated += onLocationUpdated;
        locationManager.DistanceFilter = 1;
        locationManager.DesiredAccuracy = 1;
    }

    public CLLocationManager LManager
    {
        get { return locationManager; }
    }

    public void startLocationUpdates()
    {
        if (CLLocationManager.LocationServicesEnabled)
        {
            locationManager.StartUpdatingLocation();
        }
    }

    public void stopLocationUpdates()
    {
        locationManager.StopUpdatingLocation();
    }

    public void onLocationUpdated(object sender, CLLocationsUpdatedEventArgs e)
    {
        // stop location updates until timer calls StartUpdatingLocation
        locationManager.StopUpdatingLocation();

        var coordinate = e.Locations[e.Locations.Length - 1].Coordinate;
        double lat = coordinate.Latitude;
        double lon = coordinate.Longitude;

        // do stuff with coordinates

        // This will work until the application goes into the background.
        // The same thing seems to happen whenever I try creating
        // new threads, or any other form of a timer.
        //
        // Without any sort of timer or new thread, it will call
        // StartUpdatingLocation in the background and will
        // continue to run indefinitely.
        NSTimer t = NSTimer.CreateScheduledTimer(TimeSpan.FromMilliseconds(serviceMillisecondTimeout), delegate
        {
            if (CLLocationManager.LocationServicesEnabled)
            {
                locationManager.StartUpdatingLocation();
            }
        });
    }
}
公共类位置管理器
{
受保护的CLLocationManager locationManager;
/// 
///位置检查之间的时间(毫秒)。
/// 
私有int-serviceMillistecondTimeout=10000;
公共场所经理()
{
this.locationManager=新的CLLocationManager();
//iOS 8附加权限要求
if(UIDevice.CurrentDevice.CheckSystemVersion(8,0))
{
locationManager.RequestAlwaysAuthorization();
}
locationManager.LocationsUpdated+=onLocationUpdated;
locationManager.DistanceFilter=1;
locationManager.DesiredAccuracy=1;
}
公共位置管理器
{
获取{return locationManager;}
}
public-void-ocationupdates()
{
如果(CLLocationManager.LocationServicesEnabled)
{
locationManager.StartUpdatingLocation();
}
}
公共void stopLocationUpdates()
{
locationManager.StopUpdateingLocation();
}
public void onLocationUpdated(对象发送方,CLLocationsUpdatedEventArgs e)
{
//停止位置更新,直到计时器调用StartUpdatingLocation
locationManager.StopUpdateingLocation();
var坐标=e.Locations[e.Locations.Length-1]。坐标;
双纬度=坐标纬度;
双经度=坐标经度;
//用坐标做东西
//这将一直工作到应用程序进入后台。
//每当我尝试创作时,似乎都会发生同样的事情
//新线程,或任何其他形式的计时器。
//
//如果没有任何类型的计时器或新线程,它将调用
//StartupDating在后台的位置,并将
//继续无限期运行。
NSTimer t=NSTimer.CreateScheduledTimer(TimeSpan.FromMillicles(ServiceMillicondTimeout),委托
{
如果(CLLocationManager.LocationServicesEnabled)
{
locationManager.StartUpdatingLocation();
}
});
}
}

如何设置明确的时间间隔并仍然能够在后台运行位置更新?

平台不支持此功能。您可以在后台或离开设置的地理围栏时订阅重要的位置更改,但不能强制代码在后台以特定的间隔获得位置修复。这是iOS平台的限制,您对此无能为力


最接近你想要的东西是可能的。

是的,我知道了。幸运的是,我真正需要的是一个地理围栏。让它工作起来是小菜一碟。@JaredPrice你能提供我在过去三天里试图解决这个问题的样本吗