IT tutorials
 
Mobile
 

Windows Phone 8 : Understanding the Application Execution Model - Running Under the Lock Screen - Lock Screen Management

3/28/2014 3:55:32 AM
- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019

Users expect some kinds of apps to run under a locked 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.


Note

Running under the lock screen is not an alternative to implementing efficient transient state persistency. Recall that an app may still be tombstoned if the phone runs low on memory.


The following steps outline how to enable your app to run under the locked screen:

1. Set PhoneApplicationService.Current.ApplicationIdleDetectionMode = IdleDetectionMode.Disabled.

2. Detect when the lock screen is engaged, or disengaged, by handling the PhoneApplicationFrame.Obscured and Unobscured events, respectively.

3. When the lock screen is engaged your app should reduce its processing to a bare minimum to minimize CPU usage and thus battery consumption.

4. When the lock screen is disengaged your app should resume from where it left off.

5. 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.


Tip

To engage the lock screen within the Windows Phone emulator, press F12 on the computer keyboard twice. F12 emulates the power button on the phone. Pressing it once locks the phone and turns off the display; twice turns the display on again with the lock screen engaged. The mouse can be used to disengage the lock screen by sliding the lock screen up.

To engage the lock screen when debugging on an actual device (or on the emulator), use the Simulation Dashboard, which can be opened by selecting Simulation Dashboard from the Tools menu in Visual Studio.

Lock Screen Management

I created a reusable class called LockScreenService that makes it easy to manage your app’s lock screen policy. The class implements a custom ILockScreenService interface that has the following three properties:

- RunningUnderLockScreenGets a value indicating whether the app is running under the lock screen

- RunningUnderLockScreenEnabledAllows you to set whether the app is allowed to run under the lock screen

- UserPromptedAllows 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 LockScreenService, and when the RunningUnderLockScreen property changes, it indicates that the lock screen has been either engaged or disengaged. LockScreenService is a singleton and subclasses NotifyPropertyChangeBase for property change notification (see Listing 1). The private constructor attempts to retrieve the 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 LockScreenService class is located in the Shell directory of the WPUnleashed project.

LISTING 1. LockScreenService Class (excerpt)


public class LockScreenService : NotifyPropertyChangeBase, ILockScreenService
{
    static readonly string promptedKey
               = "UserPromptedToAllowRunningUnderLockScreen";
    static readonly string runningEnabledKey = "RunningUnderLockScreenEnabled";

    LockScreenService()
    {
        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(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 LockScreenService, and are located in the ExecutionModel directory of the WPUnleashed.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 2).

LISTING 2. LockScreenViewModel Class

public class LockScreenViewModel : ViewModelBase
{
    public LockScreenViewModel() : base("lock screen settings")
    {
        LockScreenService lockScreenService = LockScreenService.Instance;

        if (!lockScreenService.UserPrompted)
        {
            bool allow = MessageService.AskYesNoQuestion(
                "Is it OK to run under the phone's lock screen?");
            lockScreenService.RunningUnderLockScreenEnabled = allow;
            lockScreenService.UserPrompted = true;
        }

        lockScreenService.PropertyChanged
            += (o, args) =>
            {
                if (args.PropertyName == "RunningUnderLockScreen")
                {
                    Debug.WriteLine("RunningUnderLockScreen: "
                         + lockScreenService.RunningUnderLockScreen);
                }
            };
    }

    public bool RunningUnderLockScreenEnabled
    {
        get
        {
            return LockScreenService.Instance.RunningUnderLockScreenEnabled;
        }
        set
        {
            LockScreenService.Instance.RunningUnderLockScreenEnabled = value;
        }
    }
}


The LockScreenView XAML has a Windows Phone 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 1 shows the ToggleSwitch located on the LockScreenView page with the Run Under Lock Screen setting enabled.

Image

FIGURE 1 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.


 
Others
 
- Windows Phone 8 : Understanding the Application Execution Model - Implementing Fast App Resume - Optimizing the Resume Experience
- Windows Phone 8 : Exploring the Execution Model (part 4) - Restoring Transient State, Saving Persistent State
- Windows Phone 8 : Exploring the Execution Model (part 3) - Programmatically Exiting an App , Saving Transient State
- Windows Phone 8 : Exploring the Execution Model (part 2) - Life Cycle Events
- Windows Phone 8 : Exploring the Execution Model (part 1) - Application State
- Windows Phone 7 to Windows Phone 8 : App publication (part 7) - Selective targeting - Device memory
- Windows Phone 7 to Windows Phone 8 : App publication (part 6) - Selective targeting - Device capabilities
- Windows Phone 7 to Windows Phone 8 : App publication (part 5) - Beta testing, Versions
- Windows Phone 7 to Windows Phone 8 : App publication (part 4) - Updates
- Windows Phone 7 to Windows Phone 8 : App publication (part 3) - Dev Center reports
 
 
Top 10
 
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
Technology FAQ
- Is possible to just to use a wireless router to extend wireless access to wireless access points?
- Ruby - Insert Struct to MySql
- how to find my Symantec pcAnywhere serial number
- About direct X / Open GL issue
- How to determine eclipse version?
- What SAN cert Exchange 2010 for UM, OA?
- How do I populate a SQL Express table from Excel file?
- code for express check out with Paypal.
- Problem with Templated User Control
- ShellExecute SW_HIDE
programming4us programming4us