Dan Sewell
Dan Sewell

Reputation: 1290

Updating Live tile using background agent

I am looking into ways of updating a live tile frequently - for example every 5 minutes.

I have used Push notifcations before, but I want to avoid them this time.

ShellTileShedule only updates once an hour.

I am thinking about using a background agent that runs every five minutes and updates the tile with the information it has obtained from the server.

Apart from the 14day expiry, can you see any pitfalls with this?

How do people normally get round the 14day expiry?

Will querying a service and downloading a few lines of text every 5 mins really kill the battery?

EDIT: It seems background agents for a task such as this only update every 30mins, is there a way round this, or a better solution?

Thanks.

Upvotes: 1

Views: 2171

Answers (2)

Mikhail  Dudin
Mikhail Dudin

Reputation: 494

I know this is a very old question, but still it might save someone's time. According to this article, every time you call Update(shellTileData) your tile's expiration time is extended for 2 weeks more. So as far as I understand, this means your app can update its tile forever without having to re-register the task.

Upvotes: 0

MrMDavidson
MrMDavidson

Reputation: 2735

You are somewhat constrained as to when your background tasks run on the phone. The OS will also move the scheduled time of the tasks if it can execute multiple at the same time - to avoid having to wake up the phone twice. If you're wanting to update the tile every 5 minutes then push notifications are your only option. But you should probably be considering if the user will actually look at information that regularly.

I go for a halfway approach - I use a PeriodicTask to update my shell tiles as the OS allows but then when the application launches I manually refresh the tiles with the latest information. This allows the user to "force" the tile data and additionally provides more realistic data after the user has exited the application.

To get around the 14 day expiry re-register your background task on every launch of your application. That'll keep pushing the 14 days out. It's intended to prevent unused applications from using precious resources - if your user is launching your application they probably still want the background agent to run. And if not, they can disable it through Settings > Applications > Background Tasks (or by uninstalling, obviously). To register your task execute something like the following in initialisation code;

  PeriodicTask task = ScheduledActionService.Find("MyTaskName") as PeriodicTask;
  if ((task != null) && (task.IsEnabled == true)) {
    ScheduledActionService.Remove("MyTaskName");
  }

  task = new PeriodicTask("MyTaskName") {
    Description = "My Periodic Task",
  };
  ScheduledActionService.Add(task);

#if DEBUG
  if (System.Diagnostics.Debugger.IsAttached == true) {
    ScheduledActionService.LaunchForTest("MyTaskName", TimeSpan.FromSeconds(10));
  }
#endif

The #if DEBUG allows you to schedule the task straight after execution for testing scenarios.

Upvotes: 1

Related Questions