了解Windows Universal App(UWP)中的扩展执行会话 [英] Understanding extended execution session in Windows Universal App (UWP)

查看:102
本文介绍了了解Windows Universal App(UWP)中的扩展执行会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力争取获得扩展执行会话,以便为我的Windows Universal应用程序工作.

I'm struggling to get extended execution session to work for my Windows Universal app.

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

What I'm trying to achieve is the following scenario. The user wants to connect the phone with a device via Bluetooth. Currently, whenever the screen turns off or an incoming call comes or the user minimizes the app, the connection fails and we restart it on resume. I'd like to enable extended execution for this in-progress connection, such that the app continues the work even when minimized (suspended).

我认为我需要ExtendedExecutionReason.Unspecified,因为它应该允许我的应用在挂起状态下运行长达10分钟(对于我的目的来说这绰绰有余).但是,连接似乎总是在挂起时失败(并且当我尝试使用VS的Application Lifecycle下拉列表尝试从调试器更改应用程序的状态时,我被ExtendedExecutionRevokedReason.SystemPolicy吊销了).我已经为我的应用启用了所有电池和后台权限,但效果仍然不佳.

I've figured I need the ExtendedExecutionReason.Unspecified, as it should allow my app to run up to 10 minutes in the suspended state (which is more than enough for my purpose). However, connection always seems to fail on suspending (and I get revoked with ExtendedExecutionRevokedReason.SystemPolicy when I try to alter the state of my app from the debugger using the Application Lifecycle dropdown of VS). I have enabled all battery and background permissions for my app, but still no good.

我的代码大致如下:

        ... (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;
    }

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

Any tips on what I am doing wrongly, how exactly the Extended Execution Session is meant to be used or how to debug the lifecycle of the app (without getting the system policy revocation) would be greatly appreciated. Thanks!

推荐答案

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

In this case "Suspend" lifecycle event in VS simulates suspension due to low resources, so that's why you get as a reason "SystemPolicy".

如果在没有附加调试器的情况下运行应用程序,则将其最小化(桌面)或切换到另一个应用程序(电话),将继续执行,但是(!)当您恢复应用程序时,会话将被恢复"原因所吊销,并且您必须通过调用RequestExtensionAsync()方法来重新开始会话.

If you run your application without attached debugger, you minimize it (desktop) or switch to another application (phone) the execution will continue, however (!) when you resume your application the session is revoked with "Resumed" reason and you have to start your session again by calling RequestExtensionAsync() method.

此技术可确保您在需要的时候执行活动.

This technique ensures that your execution will be active as long as you need.

这篇关于了解Windows Universal App(UWP)中的扩展执行会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆