了解Windows Universal App(UWP)中的扩展执行会话

时间:2016-05-24 11:01:25

标签: c# .net windows mobile uwp

我正在努力让扩展执行会话为我的Windows Universal应用程序工作。

我想要实现的是以下场景。用户希望通过蓝牙将手机与设备连接。目前,每当屏幕关闭或来电或用户最小化应用程序时,连接失败,我们在恢复时重新启动它。我想为这个正在进行的连接启用扩展执行,这样即使最小化(暂停),应用程序也会继续工作。

我认为我需要ExtendedExecutionReason.Unspecified,因为它应该允许我的应用在暂停状态下运行长达10分钟(这对我来说已经足够了)。但是,挂起时连接似乎总是失败(当我尝试使用VS的应用程序生命周期下拉列表从调试器更改我的应用程序状态时,我被ExtendedExecutionRevokedReason.SystemPolicy撤销。我为我的应用启用了所有电池和后台权限,但仍然不行。

我的代码大致如下:

        ... (on begin connection)
        ClearExtendedExcecution();

        DisplayRequest dr = new DisplayRequest();
        var newSession = new ExtendedExecutionSession
        {
            Reason = ExtendedExecutionReason.Unspecified,
            Description = "Pair device"
        };
        newSession.Revoked += SessionRevoked;
        ExtendedExecutionResult result = await newSession.RequestExtensionAsync();

        dr.RequestActive();
        try
        {
            switch (result)
            {
                case ExtendedExecutionResult.Allowed:
                    m_extendedExecutionSession = newSession;
                    // DO WORK HERE
                    await ConnectDeviceAsync(token);
                    break;
                default:
                case ExtendedExecutionResult.Denied:
                    // fallback to request screen active if extended execution fails, but still do work
                    newSession.Dispose();
                    await ConnectDeviceAsync(token);
                    break;
            }
        }
        finally
        {
            dr.RequestRelease();
            ClearExtendedExcecution();
        }
        ... 


    private async void SessionRevoked(object sender, ExtendedExecutionRevokedEventArgs args)
    {
        await CoreApplication.MainView.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
        {
            switch (args.Reason)
            {
                case ExtendedExecutionRevokedReason.Resumed:
                    Logger.Debug("Extended execution revoked due to returning to foreground.");
                    break;

                case ExtendedExecutionRevokedReason.SystemPolicy:
                    Logger.Debug("Extended execution revoked due to system policy.");
                    break;
            }
        });
    }

    private void ClearExtendedExcecution()
    {
        if (m_extendedExecutionSession == null)
        {
            return;
        }

        m_extendedExecutionSession.Revoked -= SessionRevoked;
        m_extendedExecutionSession.Dispose();
        m_extendedExecutionSession = null;
    }

有关我正在做错的提示,如何使用扩展执行会话或如何调试应用程序的生命周期(未获得系统策略撤销)的任何提示将不胜感激。谢谢!

2 个答案:

答案 0 :(得分:6)

在这种情况下,VS中的“Suspend”生命周期事件由于资源不足而模拟暂停,因此这就是为什么你得到“SystemPolicy”的原因。

如果您在没有连接调试器的情况下运行应用程序,则将其最小化(桌面)或切换到另一个应用程序(电话),执行将继续,但是(!)当您恢复应用程序时,会话因“恢复”原因而被撤销,您必须通过调用RequestExtensionAsync()方法再次启动会话。

此技术可确保只要您需要执行即可执行。

答案 1 :(得分:0)

以下是有关扩展执行的教程:https://docs.microsoft.com/en-us/windows/uwp/launch-resume/run-minimized-with-extended-execution

以下是扩展执行的示例集:https://github.com/Microsoft/Windows-universal-samples/tree/master/Samples/ExtendedExecution

您是对的,最好在应用程序处于运行状态时使用未指定的扩展执行或位置跟踪,而不是在处于暂停状态时保存数据。对于您的特定情况,听起来您正在尝试从Windows Phone继续与蓝牙设备进行通信。根据蓝牙设备是否为BLE或更早,GattCharacteristicNotificationTrigger或RfCommConnectionTrigger可能是更好的选择。它们不要求您的应用在后台持续运行,而是在外围通信期间唤醒您的应用。 Kiran对去年的Build会议中不同类型蓝牙触发器的概述提供了丰富的信息,可在此处获取:https://channel9.msdn.com/Events/Build/2016/P460