Windows Store App中判断运行的app应用是否有用户操作

对于有些Line of Business (LOB)的应用,可能为了保护一些屏幕上显示的敏感信息,会有监测用户空闲时间并锁定当前屏幕的需求。对此Windows一直有提供“屏幕保护”并在唤醒时回到登录界面的功能,但是毕竟这个设置以及空闲时间并不能通过WinRT的API进行设置,那么我们如何才能做到在应用级别内做到呢?

这个解决方案主要是解决两个问题,先是需要来检测人为操作,包括鼠标键盘和触摸输入,其次就是加入一个内建的timer来检测空闲时间,并触发timeout之后的下一步逻辑,如锁定屏幕、弹出提示等等。

对于前者,我们可以通过Window.Current.CoreWindow.KeyDown以及Window.Current.CoreWindow.PointerMoved来在app全局范围内进行事件检测。而对于timer逻辑,我这里简单地包装了一个类,可以根据需要在app级别或者page级别来加入,并自定timeout之后的动作。

 public class AppIdleDetector
    {
        public TimeSpan IdleTime
        {
            get { return DateTime.Now - IdleStartedTime; }
        }        
  
        public void Start()
        {
            UnhookCoreWindowsEvents();
            HookCoreWindowsEvents();
            ResetIdleTimer();
        }
  
        public void Stop()
        {
            UnhookCoreWindowsEvents();
        }
  
        private DateTime IdleStartedTime;
  
  
        private void ResetIdleTimer()
        {
            IdleStartedTime = DateTime.Now;
        }
  
        private void HookCoreWindowsEvents()
        {
            Window.Current.CoreWindow.KeyDown += CoreWindow_KeyDown;
            Window.Current.CoreWindow.PointerMoved += CoreWindow_PointerMoved;
        }
  
        private void UnhookCoreWindowsEvents()
        {
            Window.Current.CoreWindow.KeyDown -= CoreWindow_KeyDown;
            Window.Current.CoreWindow.PointerMoved -= CoreWindow_PointerMoved;
        }
  
        public void RegisterActionOnIdleTimer(Action action, int secondsOnIdle)
        {
            DispatcherTimer timer = new DispatcherTimer();
  
            timer.Interval = TimeSpan.FromSeconds(1);
  
            timer.Tick += new EventHandler<object>((sender, eventArgs) => {
  
                if (this.IdleTime.Seconds >= secondsOnIdle)
                {
                    action();
                    timer.Stop();
                }
  
            });
  
            timer.Start();
        }
  
        void CoreWindow_PointerMoved(Windows.UI.Core.CoreWindow sender, Windows.UI.Core.PointerEventArgs args)
        {
            ResetIdleTimer();
        }
  
        void CoreWindow_KeyDown(Windows.UI.Core.CoreWindow sender, Windows.UI.Core.KeyEventArgs args)
        {
            ResetIdleTimer();
        }
  
    }

通过public void RegisterActionOnIdleTimer(Action action, int secondsOnIdle) 方法,当前的这个IdleTimer支持加入Action来定义到点之后的动作以及设定空闲时间。一个典型的应用场景就是在app.xaml.cs中加入全局检测,到点之后弹出一个MessageDialog,而MessageDialog关闭之后,重置IdleTimer,继续空闲检测 -

 namespace InkDemo
 {
  
     sealed partial class App : Application
     {
  
         public App()
         {
             this.InitializeComponent();
             this.Suspending += OnSuspending;
             this.IdleDetector = new AppIdleDetector();
         }
  
  
         protected override void OnLaunched(LaunchActivatedEventArgs args)
         {
             //省略app模板默认代码
             Window.Current.Activate();
             registerActionOnIdleTimer();//注册IdleTimer
             Window.Current.Activated += Current_Activated; 
         }
  
  
         void registerActionOnIdleTimer()
         {
             IdleDetector.RegisterActionOnIdleTimer(async () =>
             {
                 Debug.WriteLine("on idle timer action " + DateTime.Now.ToString());
                 Windows.UI.Popups.MessageDialog dialog = new Windows.UI.Popups.MessageDialog
                     (IdleDetector.IdleTime.Seconds + "seconds", "idle timer");
  
                 await dialog.ShowAsync();
                 registerActionOnIdleTimer();
             }, 5);
         }
  
         void Current_Activated(object sender, Windows.UI.Core.WindowActivatedEventArgs e)
         {
             if (e.WindowActivationState == Windows.UI.Core.CoreWindowActivationState.Deactivated)
             {               
                 IdleDetector.Stop();
             }
             else
             {
                 IdleDetector.Start();
             }
         }
  
  
  
         public AppIdleDetector IdleDetector { get; set; }
  
         //省略app模板默认代码
     }
  
  
 }

同时这里我也把示例app.xaml.cs放到文章附件中供下载。

地址:

https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-60-38-SampleCodes/7282_2E00_App_2E00_xaml_2E00_cs