简体   繁体   中英

Windows store app 8.1 - Background Task not triggered

I am very new to Windows Store App and I am trying to use a background task.

I register it on a button click event this way: var builder = new BackgroundTaskBuilder();

         builder.Name = "bikePositionUpdate";
         builder.TaskEntryPoint = "BackgroundTaskGps.BikeGPSPositionUpdateBackgroundTask";
         builder.SetTrigger(new TimeTrigger(15, false)); // 

         BackgroundTaskRegistration taskRegistration = builder.Register();

So it is supposed to be launched within 15'.

Here is my task:

namespace BackgroundTaskGps
{
    public sealed class BikeGPSPositionUpdateBackgroundTask : IBackgroundTask
    {
        public void Run(IBackgroundTaskInstance taskInstance)
        {
             Debug.WriteLine("run run run");
        }
    }
} 

and here my manifest

...
      <Extensions>
        <Extension Category="windows.backgroundTasks" EntryPoint="BackgroundTaskGps.BikeGPSPositionUpdateBackgroundTask">
          <BackgroundTasks>
            <Task Type="timer" />
          </BackgroundTasks>
        </Extension>
      </Extensions>
...

But run method is never hit.

Please what could be my problem?

PS: I am testing my app on my local machine (the same development PC).

Please consider that I am not talking about Windows Phone App but Windows App

For Windows Phone You must need to call this

await BackgroundExecutionManager.RequestAccessAsync();

So the complete code is

await BackgroundExecutionManager.RequestAccessAsync();
 builder.Name = "bikePositionUpdate";
         builder.TaskEntryPoint = "BackgroundTaskGps.BikeGPSPositionUpdateBackgroundTask";
         builder.SetTrigger(new TimeTrigger(15, false)); //For Windows Phone 8.1 minimum is 15 minutes

         BackgroundTaskRegistration taskRegistration = builder.Register();

You are using TimeTrigger Task in response to a Trigger(fire). The minimum time between TimeTrigger Task must be 30 minutes. So you can perform a background task after every 30 minutes. It's similar to the Periodic Trigger in windows 8. So your Updated code line would be:

builder.SetTrigger(new TimeTrigger(30, false));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM