【问题标题】:Windows Phone 8 - Geolocator PositionChanged listener dies after some timeWindows Phone 8 - Geolocator PositionChanged 侦听器在一段时间后死亡
【发布时间】:2013-04-20 20:22:47
【问题描述】:

我在 Geolocator 对象中设置了一个 PositionChanged 侦听器

var geolocator = new Geolocator();
geolocator.PositionChanged += Geolocator_PositionChanged;

它工作了一段时间。但是经过一段时间没有用户交互(+- 4 小时)后,它会停止接收位置变化,我认为是因为 WP8 简单地杀死了它。这可能是 ms 所希望的,但这对我的应用模型来说太可怕了。

我所做的是另外设置一个 PeriodicTask 并发送位置。这运行没有问题,但如果用户改变他的位置,我无法真正跟踪它。

问题是:有没有在不需要用户交互的情况下唤醒这个地理定位器?它可以通过 PeriodicTask 甚至在 PositionChanged 中。

我已经尝试在 PositionChanged 委托中实例化一个新的 Geolocator,但这不起作用。

非常感谢任何帮助。谢谢!

【问题讨论】:

  • 如果使用 GeoCoordinateWatcher 类而不是 Geolocator 也会发生这种情况吗?
  • @anderZubi 没试过。在我看来,MS 无论如何都想弃用这个界面,不是吗?
  • 这是 .Net API 的一部分,而 Geolocator 类是新 WinPRT API 的一部分。我不知道微软对前者的未来意图,但如果你不与 Windows Store 应用共享代码,你可以使用它。
  • @JoséLeal 你有解决这个问题的办法吗??

标签: c# mobile geolocation windows-phone-8


【解决方案1】:

这似乎是某种杀死/禁用实例的 GC。

尝试使用geolocator.addEventListener("statuschanged", onStatusChanged);

当状态为Windows.Devices.Geolocation.PositionStatus.disabledWindows.Devices.Geolocation.PositionStatus.notAvailable时重启对象。

更多详情请参阅Geolocator.StatusChanged | statuschanged event

希望我能帮上忙

【讨论】:

  • 您好,感谢您的回答,但我已经尝试过了,但效果不佳。
【解决方案2】:

您是否在GetGeopositionAsync 调用中指定超时?根据this post 底部的最后一条评论,此代码修复了GetGeopositionAsync 调用未返回的问题。这可能是相关的。

public Task GetCurrentPosition()
{

    return Task.Run(() =>
    {
        if (_geolocator == null)
        {
           _geolocator = new Geolocator { DesiredAccuracy = PositionAccuracy.High };
        }

        Geoposition geoposition = null;

        ManualResetEvent syncEvent = new ManualResetEvent(initialState: false);

        IAsyncOperation asyncOp = _geolocator.GetGeopositionAsync();

        asyncOp.Completed += (asyncInfo, asyncStatus) =>
        {
           if (asyncStatus == AsyncStatus.Completed)
           {
               geoposition = asyncInfo.GetResults();
           }

           syncEvent.Set();

        };

        syncEvent.WaitOne();

        return geoposition.Coordinate;

    });

}

【讨论】:

    【解决方案3】:

    可能垃圾收集器发现它未使用并杀死或至少禁用以节省电量。

    您使用的大多数硬件资源都有方法调用

    addEventListener
    

    这是一种通知该资源的方式:“嘿!我想从你那里得到更新,所以不要死”。这是观察对象和异步获取更新的非常常见的模式。

    看看http://en.wikipedia.org/wiki/Observer_pattern

    【讨论】:

      猜你喜欢
      • 2019-10-20
      • 1970-01-01
      • 1970-01-01
      • 2011-09-28
      • 1970-01-01
      • 1970-01-01
      • 2017-07-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多