Windows 10移动应用程序扩展执行不断被拒绝

时间:2017-06-07 19:27:25

标签: c# uwp windows-10 windows-10-universal windows-10-mobile

我正在尝试在我的Windows 10 UWP应用程序上使用扩展执行来进行位置跟踪。网上有很多这样的例子。我基本上使用这个常见的代码块。

using (var session = new ExtendedExecutionSession())
{
    session.Reason = ExtendedExecutionReason.LocationTracking;
    session.Description = "Tracking your location";
    session.Revoked += NewSession_Revoked;
    var result = await session.RequestExtensionAsync();

    switch (result)
    {
        case ExtendedExecutionResult.Allowed:
            await StartLocationTrackingAsync();
            break;
        default:
            case ExtendedExecutionResult.Denied:
            //Notify user or log this.
            break;
    }
}

这个代码块在我的应用程序的Suspending事件处理程序中执行,如下所示:

public Scenario1()
{
    this.InitializeComponent();
    Application.Current.Suspending += Current_Suspending;
}

private async void Current_Suspending(object sender, Windows.ApplicationModel.SuspendingEventArgs e)
{
    //Do my extended execution request here.
}

我遇到的第一个问题是我甚至无法解决该事件,但是我读到了VS在调试时VS不会暂停你的应用程序。因此,我强制它暂停使用VS中的生命周期事件下拉列表。现在,我每次都会收到Suspending事件,这很好。

但是,当我请求扩展执行会话时,它每次都被拒绝。我已经尝试过模拟器和我的物理W10移动设备,每次都会多次尝试,但它被拒绝了。

这是为什么?我怎么能让它被允许?

谢谢!

1 个答案:

答案 0 :(得分:3)

假设您已在应用清单中声明位置,则必须在前台运行代码,这意味着它将在您的{{1}中有效回调但处于暂停状态。

  

位置跟踪扩展执行会话可以运行   需要。但是,每个运行只能运行一个这样的会话   设备。位置跟踪扩展执行会话只能是   在前台请求,应用程序必须处于Running状态。   这可确保用户知道应用已启动了   扩展位置跟踪会话。

要在后台运行,请参阅 -

  

仍然可以使用   应用程序在后台使用背景时的GeoLocator   任务或应用服务,无需请求位置跟踪   扩展执行会话。

当你 -

时使用MainPage_Loaded扩展执行
  

创建时指定ExtendedExecutionReason.LocationTracking   如果您的应用需要定期登录,请执行ExtendedExecutionSession   来自GeoLocator的位置。健身追踪和导航应用   需要定期监控用户的位置,并应使用此功能   原因。

以下是full article