如何在我的应用程序 运行 由于锁定屏幕而暂停时仍然拥有它,然后唤醒设备

How to still have my app run when it is suspended because of lock screen and then wake the device

我有一个 Windows UWP 应用程序,主要面向 Windows 10 Mobile。此应用程序监控用户的步行流量以及他们何时进入或离开定义的地理围栏。然后我有一个 Web 后端,它使用 SignalR 发送位置数据 to/from 设备。

问题出在锁定屏幕上。假设应用程序已启动 运行ning 但用户锁定了设备或超时锁定屏幕的设置选项过期并且设备锁定。您可以点击电源,滑动解锁,然后看到该应用程序仍在 运行ning。但是,在锁定期间,我假设该应用已暂停?

所以,我首先想知道,暂停时我的位置更新会发生什么情况?我使用这些通过 SignalR 将位置更新发送到我的集线器,但也检查它们是否是地理围栏的 in/out。我正在做这样的事情:

_geolocator = new Geolocator { ReportInterval = 0, DesiredAccuracyInMeters = 10, MovementThreshold = 4};

// Subscribe to PositionChanged event to get updated tracking positions
_geolocator.PositionChanged += OnPositionChanged;

// Subscribe to StatusChanged event to get updates of location status changes
_geolocator.StatusChanged += OnStatusChanged;

我假设当锁定屏幕接管并且我的应用程序被暂停时,我将不再获得我的 OnPositionChanged 事件?这是如何处理其他事情的,例如转弯导航,即使应用程序暂停或锁定屏幕打开,它仍会跟踪您的位置?

另外,第二个问题是如何处理 SignalR 连接?我希望仍然能够在 OnPositionChanged 事件触发时推出我的位置更新,就像我在应用程序未暂停时所做的那样。我也可以这样做吗?

我已经阅读了有关后台任务的信息,但似乎只有 运行 每隔 15 分钟执行一次(除非我误解了并且这是可配置的)。对于我的申请,15 分钟太长了。我最想要的是 30 秒或 1 分钟。

顺便说一句,是的,我知道所有这些都会对电池寿命产生严重影响。假设我对此不关心电池寿命。

谢谢!

The issue is with the lock screen. Let's say that the app is started and running but the user locks the device OR the setting option for timeout to lock the screen expires and the device locks. You could hit power, swipe to unlock and see that the app is still running. However, during the time it is locked, I assume that the app is suspended?

当您最小化应用程序时 Windows 等待几秒钟,看看用户是否会切换回它。如果您没有在这段时间 window 内切换回来,并且没有扩展执行、后台任务或 activity 赞助执行处于活动状态,Windows suspends the app. An app is also suspended when the lock screen appears as long as no extended execution session, etc. is active in that app. For more detail you could refer to Windows 10 universal Windows platform (UWP) app lifecycle.

I assume that when the lock screen has taken over and my app is suspended, I will no longer get my OnPositionChanged events? How is this handled with other things like turn by turn navigation where it will still track your location even if the app is suspended or the lock screen is on?

根据您的要求,您可以 运行 使用 ExtendedExecutionReason.LocationTracking 最小化您的应用。比如你想实现turn by turn导航,可以参考下面的代码。

Specify ExtendedExecutionReason.LocationTracking when you create an ExtendedExecutionSession if your app needs to regularly log the location from the GeoLocator. Apps for fitness tracking and navigation that need to regularly monitor the user's location and should use this reason.

详情请参考Track the user's location and App Lifecycle - Keep Apps Alive with Background Tasks and Extended Execution

private async void StartTbTNavigationSession()
{
  using (var session = new ExtendedExecutionSession())
  {
    session.Reason = ExtendedExecutionReason.LocationTracking;
    session.Description = "Turn By Turn Navigation";
    session.Revoked += session_Revoked;
    var result = await session.RequestExtensionAsync();
    if (result == ExtendedExecutionResult.Denied
    {
      ShowUserWarning("Background location tracking not available");
    }
    // Do Navigation
    var completionTime = await DoNavigationSessionAsync(session);
  }
}