- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在尝试向我的应用程序添加一项功能,该功能需要在设定的时间间隔内获取用户的当前位置。问题是每当我使用 System.Threading.Timer
时,都会创建一个新的 System.Threading.Thread
并使用 Thread.Sleep()
,或使用 NSTimer.CreateScheduledTimer
应用程序将在应用程序处于后台时停止运行 CLLocationManager
。
位置管理器.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();
}
});
}
}
我该怎么做才能设置明确的时间间隔并仍然能够在后台运行位置更新?
最佳答案
平台不支持此功能。您可以在后台或离开设置的地理围栏时订阅重要的位置更改,但您不能强制您的代码在后台以特定时间间隔获取位置修复。这是 iOS 平台的限制/限制,您无能为力。
最接近您想要的可能是 Getting Location Events in the Background .
关于ios - Xamarin iOS - CLLocationManager 在后台以设定的时间间隔获取位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31949539/
我试图根据表格看起来像这样的状态代码来查找表格中的空白。 状态表: StateID (PK) | Code -------------------- 1 | AK 2
我有一个配对字符串列表。我想找到两个字母之间的长度/间隔。到目前为止,我可以使用找到有序字母的间隔 alpha =["AM", "KQ", "ZN", "XM", "UK"] leng
我有一个配对字符串列表。我想找到两个字母之间的长度/间隔。到目前为止,我可以使用找到有序字母的间隔 alpha =["AM", "KQ", "ZN", "XM", "UK"] leng
我正在努力弄清楚如何将时间选择器的下拉间隔设置为 15 分钟间隔。默认为 30 分钟 atm。让它工作的正确调用/符号是什么?我已经尝试了很多将它们放入 '' 的变体,但没有任何进展。谢谢! $
假设我有 table teach_subject(teacher_id, subject_id, min_grade_of_school, max_grade_of_school, color_in_
我有下面的图像,我试图以 3 秒的间隔一张一张地显示它们,但我无法让它工作。它继续停留在 0 并且不显示图像,帮助会很好: JavaScript: window.animate = functio
我认为这个问题类似于加权间隔调度问题,但略有不同。 假设您有一个具有开始时间和结束时间的类次 s,该类次从 s.start 开始有 n 个空位到s.end。时隙是从 s.start 到 s.end 的
我试图将一个 GeometryReader 作为按钮推到屏幕底部,但 Spacer 在这里不起作用...... 这个想法是让应用程序响应所有屏幕尺寸。 VStack { GeometryRea
我问了一个相关问题 here但意识到我在计算这个复杂的度量时花费了太多时间(目标是与随机化测试一起使用,所以速度是一个问题)。所以我决定放弃权重,只使用两个度量之间的最小距离。所以这里我有 2 个向量
我最近成立 healthcheck s 在我的 docker-compose配置。 它做得很好,我喜欢它。下面是一个典型的例子: services: app: healthcheck:
我正在 Cocoa 中使用如下设置的 NSTimer 运行 mainLoop: mainLoopTimer = [NSTimer scheduledTimerWithTimeInter
目前正在开发家庭自动化应用程序,其中有事件 API 可以在事件被触发时为我提供事件。但我想持续运行 API,以便跟踪在整个应用程序中触发的事件。还有一个主页,我在其中显示曾经发生的事件。它是一个简单的
我有一个查询应该是这样的要求: { "size": 0, "_source": [ "dateCreated" ], "query": { "bool": {
我有一个 UNIX 格式的时间字符串。我需要将该字符串四舍五入到最接近的 30 分钟间隔。 例如:我的时间是上午 9:20,而不是应该四舍五入到上午 9:30。 如果分钟数大于 30,例如上午 9:4
我有网络调用,我想定期调用它。我只想将运算符 Interval 与 flatMap 一起使用,但在间隔线程上。你能解释一下这种情况吗?我知道Interval只使用一个线程,任务是按顺序处理的。 我有
我在我的 iOS 应用程序中使用了 NSTimer,但由于 SetNeedsDisplay,我没有得到我想要的结果。 我做了一些研究并找到了 CADisplayLink,它为我提供了我想要的动画结果。
我需要通过给出值数组来生成 map 上图例的值。Java 库中是否有函数可以从值数组和计数值生成范围或区间?像这样的东西: Integer[] getIntervals(Number[] values
我的函数中有以下代码,我试图从数据库中获取参数MAX_FAILED_ATTEMPT,并且基于此,如果检查失败,我将发送警报。当前代码将尝试从 MAX_FIELD_ATTEMPT 获取值并立即依次进行检
我在这里要做的是像 Windows XP 上的那样放下一个轨迹栏来更改分辨率:( http://puu.sh/7Li5h.png ) 我想设置特定的间隔/增量值,如上图所示。目前,实际栏下方的线条已经
是否可以停止当前作为 setInterval 运行的函数? 这是我的代码: 这是我调用的函数 function pull_light_status (lights_array) { $.get
我是一名优秀的程序员,十分优秀!