Ways of detecting Windows Phone idle

483 views Asked by At

I want to perform some calculations while the phone is in locked screen mode, regardless if the app is on the foreground or the background.

I know that in order to do this, I need to use OnObscured event from App class. However, it is only launched when the app is in the foreground, but not in the background.

So, I would like to know if exist any way to detect the phone state while the App runs the background.

I have thought something that would be crazy, but is to access an API property which is not allowed to use while the phone is in locked screen, and then catch the exception and, with that, get if the phone is active or sleeping.

I am open to hear new ideas.

2

There are 2 answers

4
Romasz On

I figured out a simple thing - maybe it will help you:
I assume that you already disabled Idle Detection to run your calculations in background.
So why not to create the variable in which you hold the state of the App? Since you have to launch first your App, so it goes to foreground and when Obscured is called and IsLocked = true, set the variable. Then you can check it whenever you want:

public MainPage()
{
   InitializeComponent();

   App.RootFrame.Obscured+=RootFrame_Obscured;
   App.RootFrame.Unobscured+=RootFrame_Unobscured;
}

private bool AppIsLocked = false;

private void RootFrame_Unobscured(object sender, EventArgs e)
{
  if (AppIsLocked) AppIsLocked = false;
}

private void RootFrame_Obscured(object sender, ObscuredEventArgs e)
{
   if (e.IsLocked) AppIsLocked = true;
}
0
crea7or On

Is this what you searching for Running a Windows Phone Application under the lock screen? This article describes - how to avoid tombstoning. But you can't do a lot of work under lock screen.