- Exploring the Execution Model
- Running Under the Lock Screen
- Page Navigation
- Walking Through the Bookshop Sample Application
- Summary
Running Under the Lock Screen
Users expect some kinds of apps to run under a lock screen. These apps include music players, mapping apps, stop watches, and so on.
In the first release of the Windows Phone OS, running under the lock screen was favorable to apps that wanted to avoid being tombstoned. These were apps that were slow to load or relied on complex state models. This was alleviated, however, with the introduction of fast application switching in Windows Phone 7.5. Now apps are placed in a dormant state and remain in memory.
The following steps outline how to enable your app to run under the lock screen:
- Set PhoneApplicationService.Current.ApplicationIdleDetectionMode = IdleDetectionMode.Disabled.
- Detect when the lock screen is engaged, or disengaged, by handling the PhoneApplicationFrame.Obscured and Unobscured events, respectively.
- When the lock screen is engaged your app should reduce its processing to a bare minimum to minimize CPU usage and thus battery consumption.
- When the lock screen is disengaged your app should resume from where it left off.
- Optionally, you should prompt the user to allow him or her to opt-in to running under the lock screen, and/or provide an options setting for enabling or disabling running under the lock screen.
Lock Screen Management
I created a reusable class called LockScreenManager that makes it easy to manage your app’s lock screen policy. The class implements a custom ILockScreenManager interface that has the following three properties:
- RunningUnderLockScreen—Gets a value indicating whether the app is running under the lock screen
- RunningUnderLockScreenEnabled—Allows you to set whether the app is allowed to run under the lock screen
- UserPrompted—Allows your app to remember whether the user has been prompted to allow running under the lock screen
At your app’s first launch you query the UserPrompted property. If false, you present a dialog asking the user whether it is okay to run under the lock screen, and you set the RunningUnderLockScreenEnabled property accordingly. You subscribe to the PropertyChanged event of the LockScreenManager, and when the RunningUnderLockScreen property changes, it indicates that the lock screen has been either engaged or disengaged. LockScreenManager is a singleton and subclasses NotifyPropertyChangeBase for property change notification (see Listing 3.1). The private constructor attempts to retrieve the tt>UserPrompted and RunningUnderLockScreenEnabled property values from isolated storage settings. It then subscribes to the PhoneApplicationFrame.Obscured and Unobscured events.
When the Obscured event is raised the RunningUnderLockScreen property is set.
The LockScreenManager class is located in the Shell directory of the WindowsPhone7Unleashed project.
Listing 3.1. LockScreenManager Class (excerpt)
public class
LockScreenManager
:NotifyPropertyChangeBase
,ILockScreenManager
{static readonly string
promptedKey ="UserPromptedToAllowRunningUnderLockScreen"
;static readonly string
runningEnabledKey ="RunningUnderLockScreenEnabled"
; LockScreenManager() {IsolatedStorageSettings
settings =IsolatedStorageSettings
.ApplicationSettings;bool
prompted;if
(settings.TryGetValue(promptedKey,out
prompted)) { UserPrompted = prompted; }bool
enabledValue;if
(settings.TryGetValue(runningEnabledKey,out
enabledValue)) { RunningUnderLockScreenEnabled = enabledValue; }var
frame = (PhoneApplicationFrame
)Application
.Current.RootVisual; frame.Obscured += (o, args) => RunningUnderLockScreen = args.IsLocked; frame.Unobscured += (o, args) => RunningUnderLockScreen =false
; } ... }
When either of the UserPrompted or RunningUnderLockScreenEnabled properties is set, its new value is saved to isolated storage settings using a SaveSetting method, as shown:
void
SaveSetting(string
key,object
value) {IsolatedStorageSettings
settings =IsolatedStorageSettings
.ApplicationSettings; settings[key] = value; settings.Save(); }
When the RunningUnderLockScreenEnabled property is enabled the idle detection mode is disabled, which allows the app to run under the lock screen. If disabled, the app must be restarted or deactivated before the idle detection mode can be enabled or an InvalidOperationException is raised. This is a limitation of the phone OS. See the following excerpt:
public bool
RunningUnderLockScreenEnabled {get
{return
runningUnderLockScreenEnabled; }set
{var
result = Assign(() => RunningUnderLockScreenEnabled,ref
runningUnderLockScreenEnabled,value
);if
(result ==AssignmentResult
.Success) {if
(runningUnderLockScreenEnabled) {PhoneApplicationService
.Current.ApplicationIdleDetectionMode =IdleDetectionMode
.Disabled; }/* Idle detection mode cannot be enabled
until the application is restarted. */
SaveSetting(runningEnabledKey, runningUnderLockScreenEnabled); } } }
The LockScreenView page and its associated LockScreenViewModel class demonstrate the use of the LockScreenManager, and are located in the ExecutionModel directory of the WindowsPhone7Unleashed.Examples project. The LockScreenViewModel uses the MessageService to ask the user whether she wants to opt-in to running under the lock screen. When the manager’s RunningUnderLockScreen property changes, a string is written to the Visual Studio Output view (see Listing 3.2).
Listing 3.2. LockScreenViewModel Class
public class
LockScreenViewModel
:ViewModelBase
{public
LockScreenViewModel() :base
("lock screen settings"
) {LockScreenManager
manager =LockScreenManager
.Instance;if
(!manager.UserPrompted) {bool
allow = MessageService.AskYesNoQuestion("Is it OK to run under the phone's lock screen?"
); manager.RunningUnderLockScreenEnabled = allow; manager.UserPrompted= true;
} manager.PropertyChanged += (o, args) => {if
(args.PropertyName =="RunningUnderLockScreen"
) {Debug
.WriteLine("RunningUnderLockScreen: "
+ manager.RunningUnderLockScreen); } }; }public bool
RunningUnderLockScreenEnabled {get
{return
LockScreenManager
.Instance.RunningUnderLockScreenEnabled; }set
{LockScreenManager
.Instance.RunningUnderLockScreenEnabled =value;
} } }
The LockScreenView XAML has a Silverlight Toolkit ToggleSwitch control that is bound to the RunningUnderLockScreenEnabled viewmodel property, as shown in the following excerpt:
<
StackPanel
x
:
Name
="ContentPanel">
<
toolkit
:
ToggleSwitch
Header
="run under lock screen"
IsChecked
="{
Binding
RunningUnderLockScreenEnabled
,
Mode
=TwoWay}" />
</
StackPanel
>
Figure 3.3 shows the ToggleSwitch located on the LockScreenView page with the Run Under Lock Screen setting enabled.
Figure 3.3. LockScreenView page
Running under the lock screen is the only way to allow your foreground app to run while the phone is idle. It should, however, be used with caution, because if an app continues to consume the device CPU, it may rapidly flatten the device battery.