Search code examples
windows-runtimewindows-phone-8.1background-task

Is it possible for a BackgroundTask to know if an app is currently in the foreground? If so, how?


I have a background task that runs when a raw notification is received.

If the app is not in the foreground I want to display a toast notification, but if it is in the foreground, I don't want to display a notification.

Is it possible to test if the app is in the foreground from a BackgroundTask? If so, how?


Solution

  • You need to store some variable in IsolatedStorage and change it's value to true when the app is in the foreground. After, when your apps go to background you need to change that variable value to false.

    In your background task you need to check the value of this variable in isolated storege. And of course you need to protect your code (one way is to use mutex) to prevent access errors.

    This is the simpliest and recommended way to communicate between App and BackgroundTask. No 'from the box' mechanism is provided for such communication so you have to implement your own.