Idle detection for Windows Phone 8

[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]

The Windows Phone operating system can take actions to reduce power consumption on the device when it detects that the user or the current app is in an idle state. Depending on the type of app you are creating, you may need to disable idle detection for users or apps. This topic explains how to modify the operating system’s idle behavior.

Important Note:

This Windows Phone feature has certification requirements that must be met for an app to be eligible for listing in the Windows Phone Store. You should consider these requirements as you design and test your app. For more information, see App certification requirements for Windows Phone.

This topic contains the following sections.

Changing the app idle detection mode

In the current release, an app is considered to be idle if the user has allowed the phone to lock. In future releases, the conditions that qualify an app as idle may change. When app idle detection is enabled, the operating system will deactivate idle apps. Disabling app idle detection requests that the operating system not perform this service. Disable app idle detection by setting the ApplicationIdleDetectionMode property of the app’s PhoneApplicationService object to Disabled. Even if app idle detection is disabled, the operating system may deactivate an app for other reasons, such as a depleted battery.

Don’t disable idle detection to speed up the resume time of your app after the phone is unlocked or to maintain the app state while the phone is locked. Windows Phone apps go into a dormant state when the lock screen engages and, when the phone unlocks, quickly resume to exactly the same state in which they entered dormancy. This is all accomplished automatically and without the drain on the battery or potential hidden data plan cost to the user that disabling idle detection introduces.

Valid reasons to disable idle detection in Windows Phone apps include ones where core functionality continues while the phone is locked (for instance, an exercise tracking app) or that play back audio under lock in a fashion not supported by a background agent.

Warning

Don’t disable app idle detection to speed up app resume. Doing so won’t improve performance or app state maintenance, but will cause your app to drain the device battery more quickly, causing a negative user experience.

Checklist for apps that disable app idle detection

If your Windows Phone app disables app idle detection to run under the lock screen, you should perform the following tasks to reduce your app’s power consumption while the lock screen is engaged.

  1. Implement the Obscured event handler of the app’s root PhoneApplicationFrame object. This event is raised when the lock screen is engaged or when some other action obscures the running app’s frame, such as an incoming phone call. Perform the following steps in the Obscured event handler.

  2. Stop all active timers using Timer..::.Dispose and DispatcherTimer..::.Stop.

  3. Stop all animations by calling Storyboard..::.Pause or Storyboard..::.Stop.

  4. Stop the accelerometer by calling Stop()()().

  5. Stop the Location Service by calling Stop()()().

  6. Turn off the FM radio by setting the PowerMode property to Off.

  7. Signal your app that new network requests and isolated storage operations shouldn’t be performed.

  8. Implement the Unobscured event handler. This event is raised when the lock screen is disengaged. In this event handler, you should restart anything that you stopped in the Obscured event handler.

Checklist for XNA Framework games that disable app idle detection

If your XNA for Windows Phone game disables app idle detection to run under the lock screen, you should perform the following tasks to reduce your app’s power consumption while the lock screen is engaged.

  1. Signal your app to return immediately from the Microsoft.Xna.Framework.Game.Update method without performing any actions.

  2. Set the Microsoft.Xna.Framework.Game.InactiveSleepTime property to 1 second.

Other considerations when disabling app idle detection

The following list presents important information for apps that disable idle detection.

  • This feature should be used with caution because apps that run while the phone is locked continue to drain the device’s battery.

  • If your app uses a MediaElement object, you should set its MediaElement..::.Source property to null in the Obscured event handler and then restore the previous value in the Unobscured event to ensure that the MediaElement isn’t consuming resources while the lock screen is engaged.

  • The accelerometer doesn’t return data while the lock screen is engaged.

  • ApplicationIdleDetectionMode can’t be set until the PhoneApplicationFrame object has been initialized.

  • Always ask the user if they want to disable either UserIdleDetectionMode or ApplicationIdleDetectionMode before doing so. Your app shouldn’t assume that the user wants this behavior.

  • In the current release, app idle detection can’t be enabled in a single app instance after it has been disabled. Doing so throws an exception. In future releases this may be supported, so your app may choose to disable app idle detection when it’s no longer needed and catch the expected exception. The following code snippet shows an implementation of this.

            // Custom function to turn off idle detection. This will throw an exception in the current release.
            public void TryReenableApplicationIdleDetection()
            {
                bool didEnable = false;
                try
                {
                    Microsoft.Phone.Shell.PhoneApplicationService.Current.ApplicationIdleDetectionMode =
                        Microsoft.Phone.Shell.IdleDetectionMode.Enabled;
                    didEnable = true;
                }
                catch (InvalidOperationException ex)
                {
                    // This exception is expected in the current release.
                }
    
                // Possibly use the value of didEnable to decide what to do next.
                // If it is 'true', then your app will be deactivated. 
                // If it is 'false', then your app will keep running.
            }
    

Changing the user idle detection mode

Disable user idle detection by setting the UserIdleDetectionMode property of the app’s PhoneApplicationService object to Disabled. When user idle detection is enabled, the operating system will go into a low power usage state when the user is idle. Disabling this property requests that the operating system not perform this service. Use this property to disable user idle detection if your app needs to continue running even when the user isn’t physically interacting with the screen or hardware buttons. Example scenarios include turn-by-turn navigation apps and games that use the accelerometer for input.

This feature should be used with caution. Apps that disable user idle detection will continue to run and consume battery power when the user isn’t using the phone. It’s recommended that apps that disable user idle detection implement their own form of idle detection and enable UserIdleDetectionMode when appropriate. For example, an accelerometer-based game could enable user idle detection if the accelerometer shows no activity for a period of time.

The operating system considers the user to be idle when they haven’t touched the screen or the hardware buttons within the device lock timeout window, specified in the device’s Settings page. The conditions for detecting user idle status may change in future releases.