0

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.

programmer23
  • 533
  • 4
  • 15

2 Answers2

1

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;
}
Romasz
  • 29,662
  • 13
  • 79
  • 154
  • The problem is the following: If my App is running, and then I click on the Windows button of the device it will go on the background, and then if I lock the screen, my App doesn't detect it. I tried using this code but because the app is in background before locking the screen, the obscured events doesn't work. – programmer23 Jan 09 '14 at 10:20
  • When you tap start or search buton (or launchers & choosers) your App goes to dormant or tombstoned state and NOT work in background (all threads are stopped)! You have to read some more documentation http://msdn.microsoft.com/en-us/library/windowsphone/develop/ff817008%28v=vs.105%29.aspx ;) – Romasz Jan 09 '14 at 10:27
  • If you want some scheduled task or other solutions to work in background you may need a Background Agent: http://msdn.microsoft.com/en-us/library/windowsphone/develop/hh202942%28v=vs.105%29.aspx – Romasz Jan 09 '14 at 10:28
  • I have seen the ResourceIntensiveTask and it seems quite interesting. However it says that only executes for 10 minutes. I would like to know if it is possible to execute this task periodically (let's say every 30 minutes for example) – programmer23 Jan 09 '14 at 12:04
0

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.

crea7or
  • 4,421
  • 2
  • 26
  • 37