Windows 8 Live Tiles – take 2

January 28, 2013

no comments

Quite some time ago I blogged about Live Tiles & Toast Notifications Windows 8 feature.

About a week ago I was delivering Windows 8 training and as a part of this course was delivering a sessions about Live Tiles and Windows 8 PLM (PLM = Process Lifetime Management, session which discusses when Windows 8 runs apps and some point about Windows 8 background tasks). The PLM session mentions, that Windows 8 apps are permitted to run background tasks, but they will be executed approximately once in 2 hour and will be getting few seconds of CPU time. I’ve also mentioned, that some apps, which got user permission to run under the lock screen will be able to run the background tasks based on system timer, but those apps will be scheduling to run once in 15 minutes as minimum timeslot between the execution. Right after the PLM session, at the break, one of my students came to me and asked how “clock” live tiles applications (already few published in the Store) are working. I had to admit, that I never saw this apps before and was not aware about them and how do they working. The student pointed out on one of them and after installing the app I saw that the app go a live tile with current clock and it updates the tile once in a minute. I have to admit, I was intrigued…

I checked the app permissions – it used background permissions, but this was not enough to update a tile once in a minute (remember – the minimum timeslot for such apps are 15 minutes). Then (after briefly chatting about this with my friend and colleague from MS DPE) I assumed that app probably uses push notifications. After further thoughts I failed also this option as the app is free and pushing 1440 messages daily per client is way above free limit of push notifications. I double checked myself by disconnecting network connections on my laptop to make sure. The app keep working and updated the tile. And suddenly it hit me – the app uses Scheduled Toast Notifications! It is possible to schedule many future toasts (I don’t remember exact number, but way more than 15 (needed for background service with lock screen permissions) and more than 120 (needed for regular background service). I tried to repro the app and here it is – for the sake of simplicity I didn’t create images of all minutes per day (actually half is enough as analogue clock has the same presentation once in 12 hours). Also I decided to make a sample without background service as for my demo it was not rally needed.

So I’ve create the super simple UI which gets a count number from user:

<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">

    <Grid.RowDefinitions>

        <RowDefinition/>

        <RowDefinition/>

    </Grid.RowDefinitions>

    <TextBox x:Name="txtCount" Text="10" MaxHeight="40" MaxWidth="40" HorizontalAlignment="Left"/>

    <Button Content="Set" Click="Button_Click_1" Grid.Row="1"/>

</Grid>

And here is button click event handler:

private void Button_Click_1(object sender, RoutedEventArgs e)

{

    //Remove old scheduled notifications as we are starting new countdown

    var oldNotifications = TileUpdateManager.CreateTileUpdaterForApplication().GetScheduledTileNotifications();

    foreach (var item in oldNotifications)

        TileUpdateManager.CreateTileUpdaterForApplication().RemoveFromSchedule(item);

 

    //Parse user input and get current time

    int val = int.Parse(txtCount.Text);

    DateTime startTime = DateTime.Now;

 

    //Set "Ready, Set, Go!" tile - displayed immediately

    ITileSquareText01 tile0 = TileContentFactory.CreateTileSquareText01();

    tile0.TextHeading.Text = "";

    tile0.TextBody1.Text = "Ready!";

    tile0.TextBody2.Text = "Set!";

    tile0.TextBody3.Text = "Go!";

 

    TileNotification theTile0 = new TileNotification(tile0.GetXml());

    TileUpdateManager.CreateTileUpdaterForApplication().Update(theTile0);

 

    //Loop and schedule required number of tiles

    for (int i = 1; i <=val; i++)

    {

        ITileSquareBlock tile = TileContentFactory.CreateTileSquareBlock();

        //Tile main presentation is the number of current counter and will be presented to user when the tile is shown

        tile.TextBlock.Text = i.ToString();

        tile.TextSubBlock.Text = "Counting...";

 

        //Create scheduled tile notification and schedule it to run once in 5 seconds staring from now

        //Note: from my checks, it is possible to schdule the tiles once in a seconds, but some of them were skipped (about each 2-3)

        ScheduledTileNotification scheduledTile = new ScheduledTileNotification(tile.GetXml(), startTime.AddSeconds(i * 5));

        TileUpdateManager.CreateTileUpdaterForApplication().AddToSchedule(scheduledTile);

    }

 

    //Create a tile with "All done" mesage

    ITileSquareText01 tile1 = TileContentFactory.CreateTileSquareText01();

    tile1.TextHeading.Text = "All Done!";

    tile1.TextBody2.Text = "Countdown stopped";

 

    //It is scheduled to run 5 seconds after last count 😉

    ScheduledTileNotification scheduledTile1 = new ScheduledTileNotification(tile1.GetXml(), startTime.AddSeconds((val + 1) * 5));

    TileUpdateManager.CreateTileUpdaterForApplication().AddToSchedule(scheduledTile1);

}

Note: In this sample I am using NotificationsExtensions – the free windows component created by Microsoft and distributed for free as a part of Windows 8 SDK samples.

That’s it! Wow! So simple, but super creative! The video of my sample running (sorry for ugly UI):

My sample running and updating live tile every ~5 seconds

Smile

Clock app schedules the tiles using different tile template and points to images distributed with app. IMO it schedules 60-120 notifications, and every time background task runs adjust slightly the time and reschedules the tiles. I saw few times that the tile “skips” a minute sometime and jumps two minutes or stays on same minute instead of changing an image which makes me thing that at this time background task was running and adjusted an tiles schedule.

 

Enjoy and stay tuned!

//Alex

Add comment
facebook linkedin twitter email

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*