繁体   English   中英

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

[英]Understanding extended execution session in Windows Universal App (UWP)

我正在努力获取扩展执行会话以适合我的Windows Universal应用程序使用。

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

我认为我需要ExtendedExecutionReason.Unspecified ,因为它应该允许我的应用在挂起状态下运行长达10分钟(这对我来说已经足够了)。 但是,连接似乎总是在挂起时失败(并且当我尝试使用VS的Application Lifecycle下拉列表尝试从调试器更改应用程序状态时,我被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;
    }

任何有关我做错事情的提示,如何确切地使用扩展执行会话或如何调试应用程序的生命周期(而无需取消系统策略)的任何技巧都将不胜感激。 谢谢!

在这种情况下,VS中的“挂起”生命周期事件会由于资源不足而模拟挂起,因此这就是您将“ SystemPolicy”作为原因的原因。

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

此技术可确保您根据需要执行活动。

这是有关扩展执行的教程: https : //docs.microsoft.com/zh-cn/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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM