WP8 led灯在屏幕锁定下运行

时间:2014-01-15 09:35:35

标签: windows-phone-8 windows-phone

在诸如Flashlight-X,Flashlight-XT等应用程序中,实现了一项功能:在屏幕锁定下运行LED灯。 启用灯的一般实施:

var captureDevice = await AudioVideoCaptureDevice.OpenAsync(CameraSensorLocation.Back, AudioVideoCaptureDevice.GetAvailableCaptureResolutions(CameraSensorLocation.Back).First());

captureDevice.SetProperty(KnownCameraAudioVideoProperties.VideoTorchMode, VideoTorchMode.On);

此代码启动灯光,但在屏幕锁定后20秒后灯光将被禁用。 应该怎样做以保持光线在锁定下活着?

在隐藏的事件上阻止UI线程有效,但在解锁设备后不想阻止它。

有人建议禁用UserIdleDetectionMode,但它没有帮助。

1 个答案:

答案 0 :(得分:0)

以下代码可让您的应用在锁屏下运行:

    void rootFrame_Unobscured(object sender, EventArgs e)
    {

        status.Text = "coming back from lock screen!";
        // resume the application
    }

    void rootFrame_Obscured(object sender, ObscuredEventArgs e)
    {

        status.Text = "going under lock screen!";
        //minimize CPU usage, and  save the settings.
    }

    private void RunAppUnderLockscreen(object sender, RoutedEventArgs e)
    {
        MessageBox.Show("The application will run under lock screen");
        PhoneApplicationService.Current.ApplicationIdleDetectionMode = IdleDetectionMode.Disabled;
        PhoneApplicationFrame rootFrame = App.Current.RootVisual as PhoneApplicationFrame;
        if (rootFrame != null)
        {
            rootFrame.Obscured += new EventHandler<ObscuredEventArgs>(rootFrame_Obscured);
            rootFrame.Unobscured += new EventHandler(rootFrame_Unobscured);
        }
    }

请查看this MSDN blog post以获取更多有用的信息,例如:

  

请记住您的应用程序设置   ApplicationDetectionModeIdleDetectionMode.Disabled,它不能   将其更改回IdleDetecionMode.Enabled直到申请   要么再次启动(所以完全重启),要么停用   重新激活。