Home > Metro, Windows 8 > Windows 8 Lockscreen app

Windows 8 Lockscreen app

In the new app lifecycle model, all apps, which are not currently in use by the user will be terminated after 10 seconds in the background. This helps to preserve system resources like CPU cycles and battery life. However there are some cases, when apps should run in the background, even if there is no UI visible for the user.

There are different types of scenarios possible and the WinRT library has solutions for this scenarios, which i’ll describe in another post.

There is one special scenario for so called lockscreen apps.

When the user has locked his PC, then certain apps can still interact with the lockscreen and display some status information there.

Only the user can decide, which apps should be visible and active in the lockscreen and the number of apps is limited to seven plus one app for detailed information.

This app can update its tile, all others can only update the badge data. The user defines, which app are on the lockscreen and which single app can update the tile with a detailed status. For this, open the "Personalize" tab in the "Change PC settings" app.

image

 

All this apps are then visible in the red marked area in the lockscreen picture below.

The logo and the badge data, which can be a number from 1 to 99 or a symbol from here can be displayed.

The single app, which is able to display detailed information will be shown inside the yellow frame.

It can display tile data in any of this formats, which must be a wide template (2x)

image

 

Not every app can be used as lockscreen app. The apps must follow this pattern:

  • Add a badge logo with 24×24 pixel in png format to the Assets folder of the project
  • Add a wide logo with 310 x 150 pixel in png format to the Assets folder
  • Open Package.appxmanifest and select the badge and wide logo
  • Set Lock screen notification to "Badge" or "Badge and Tile text"

image

A red error logo will remind you, that you must add a background task for this kind of application. This can be done through the Declarations Tab in Package.appxmanifest

  • Select "Baxckground Tasks" from the dropdown and add it to the list.
  • Enter the fully qualified classname into the textbox labeled "Entry point"

image

On Startup we need to ask, whether the app is allowed to run as lockscreen app.

We can do this e.g. in the OnNavigatedTo() method of the MainPage:

protected async override void OnNavigatedTo(NavigationEventArgs e)
{
    BackgroundAccessStatus backgroundStatus = 
              await BackgroundExecutionManager.RequestAccessAsync();
    lblStatus.Text = backgroundStatus.ToString();
    base.OnNavigatedTo(e);
}

 

 

The user will get this question on the first startup of this app:

image

 

If the user allows to run the app in the background, then it will be added to one of the seven lockscreen slots, which can be customize by the user through opening "Personalize" tab from the "Change PC settings" app.

The app will the display one of this states from BackGroundAccessStatus enum:

public enum BackgroundAccessStatus
{
    // Summary:
    //     The user has not selected "allow" or "don't allow" or
    //     dismissed the dialog without making a choice.
    //     The app cannot perform background activity
    Unspecified = 0,
    //
    // Summary:
    //     The user chose "allow" in the dialog box. 
    //     The app is added to the lock screen,
    //     can set up background tasks, and, if it has the capability, 
    //     can use the real-time connectivity (RTC) broker. 
    //     This means that the app can function while the
    //     device is in the connected standby state.
    AllowedWithAlwaysOnRealTimeConnectivity = 1,
    //
    // Summary:
    //     The user chose "allow" in the dialog box. 
    //     The app is added to the lock screen
    //     and can set up background tasks, 
    //     but it cannot use the real-time connectivity (RTC) broker. 
    //     This means that the app might not function while the device
    //     is in connected standby.
    //     Note that apps that do not specify RTC in their manifest
    //     will always demonstrate this behavior.
    AllowedMayUseActiveRealTimeConnectivity = 2,
    //
    // Summary:
    //     The user chose "don't allow" in the dialog box. 
    //     The app is not added to the
    //     lock screen.After this value has been returned, 
    //     subsequent calls to the RequestAccessAsync method do not present 
    //     the dialog box to the user. They have stated their
    //     preference and it should be honored.
    Denied = 3,
}

When the app is running, it can update badge data using this code:

private void UpdateBadgeData(string badgeMessage)
{
    XmlDocument xmlDoc = 
      BadgeUpdateManager.GetTemplateContent(BadgeTemplateType.BadgeNumber);

    // using BadgeTemplateType.BadgeNumber or .BadgeGlyph will return:
    //<badge value=""/>

    var node = xmlDoc.SelectSingleNode("/badge");
    node.Attributes[0].NodeValue = badgeMessage;

    BadgeNotification badgeNotification = new BadgeNotification(xmlDoc);
    BadgeUpdateManager.CreateBadgeUpdaterForApplication().Update(badgeNotification);
    lblStatus.Text = "Set new badge data to Lockscreen!";
}

 

Updating the tile data must take into account, that the logo can be either small or wide.

If the app is on the lockscreen, then its always using the wide tile template, but on the regular start screen, the user can decicde which size should be used.

So both XML-templates must be contained in the update data. The code looks like this:

private void btnSendTile_Click_1(object sender, RoutedEventArgs e)
{
    //XmlDocument xmlDoc = 
    //   TileUpdateManager.GetTemplateContent(TileTemplateType.TileWideBlockAndText01);

    string template = String.Format("<tile>" + 
    " <visual>" +
    "   <binding template='TileSquareText01'>" +
    "      <text id='1'>{0}</text>" +
    "      <text id='2'>{1}</text>" +
    "   </binding>" +
    "   <binding template='TileWideText01'>" +
              "<text id='1'>{0}</text>" +
              "<text id='2'>{1}</text>" +
    "   </binding>" +
    " </visual>" +
    "</tile>", DateTime.Now.Second.ToString(), txtMessage.Text);
    XmlDocument xmlDoc = new XmlDocument();
    xmlDoc.LoadXml(template);

    TileNotification tileNotification = new TileNotification(xmlDoc);
    TileUpdater tileUpdater = TileUpdateManager.CreateTileUpdaterForApplication();
    tileUpdater.Update(tileNotification);
    lblStatus.Text = "Set new Tile logo!";
}

 

 

 .

Categories: Metro, Windows 8
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: