3

When I was searching for an answer to this, I only encountered developers looking for ways to update their apps' live tiles more and more frequently. I want the opposite, sort of.

See I'm developing a weather app, and I want it to update every hour but for a specific hourly range only. That is, I don't want the user to have the ability to update the tile once every hour because 1) people sleep and 2) the API I'm using is free only for the first 1,000 calls per day. In other words, users don't need to it update every hour and I can't afford to give them the option to anyway.

So is it possible to get, for example, the live tile to update every hour from 8am to 11pm, and to not make any calls from 12pm till 7am?

Barrrdi
  • 567
  • 7
  • 23

1 Answers1

0

If you make the call to the API in your ScheduledAgent, simply wrap the call in an if block that checks the time. I had a similar need to update the tile once a day (it was counting down the days until Xmas).

This code is in my ScheduledAgent.cs file. It checks the date (should only trigger in December and before the 26th) and sets the countdown, then sends a toast notification only on Xmas morning. It should be a good example of how to restrict API calls to a set time of dat in your background task.

if (DateTime.Now.Month == 12 && DateTime.Now.Day < 26)
{
    //number of days until the 25th
    var countdown = ((new DateTime(DateTime.Now.Year, 12, 25).DayOfYear) - DateTime.Now.DayOfYear);

    if (secondaryTile != null)
    {
        var imageString = "/Images/Tiles/" + countdown + ".png";
        var newTileData = new StandardTileData
        {
            BackgroundImage = new Uri(imageString, UriKind.Relative)
        };
        secondaryTile.Update(newTileData);
    }

    var now = DateTime.Now;
    if (now.Day == 25 && now.TimeOfDay.Hours == 9 && (now.TimeOfDay.Minutes > 14 && now.TimeOfDay.Minutes < 46))
    {
        var toast = new ShellToast { Title = "Xmas Countdown", Content = "Merry Xmas! Thank you for using 'Quick Xmas List' and have a safe holiday!" };
        toast.Show();
    }
}
Adam Benoit
  • 367
  • 1
  • 12