简体   繁体   English

Windows Phone 8.1应用中的后台任务在一天中的特定时间触发

[英]Background task trigger on a specific time of day in windows phone 8.1 app

Im developing a windows phone 8.1 WinRT c#/Xaml app that needs to download some json string on every morning. 我正在开发Windows Phone 8.1 WinRT c#/ Xaml应用程序,该应用程序需要每天早晨下载一些json字符串。

How do I get my background task to run on that specific time of the day. 如何让后台任务在一天的特定时间运行。 What trigger should I use for it ? 我应该使用什么触发器?

Please someone guide me or point me to some tutorial. 请有人指导我或为我提供一些教程。

Thank you 谢谢

This is how I solved my problem. 这就是我解决问题的方式。

public async static Task RegisterBackgroundAgentAsync(string time)
    {
        var Datetime = DateTime.ParseExact(time, "HHmm", new System.Globalization.CultureInfo("en-US"));
        var taskname = "BackgroundAgentTask";
        var builder = new BackgroundTaskBuilder();
        builder.Name = taskname;
        builder.TaskEntryPoint = "BackgroundAgent.BackgroundAgentTask";
        TimeSpan ts = Datetime - DateTime.ParseExact(DateTime.Now.Hour.ToString("00") + DateTime.Now.Minute.ToString("00"), "HHmm", new CultureInfo("en-US"));
        if (ts.TotalMinutes < 15)
        {
           if (ts.TotalMinutes < 0)
           {
               if (1440 + ts.TotalMinutes > 15)
                  builder.SetTrigger(new TimeTrigger((uint)(1440 + ts.TotalMinutes), false));
               else
                  builder.SetTrigger(new TimeTrigger(15, false));
           }
           else
           {
               if (ts.TotalMinutes == 0)
                   builder.SetTrigger(new TimeTrigger(1440, false));
               else
                   builder.SetTrigger(new TimeTrigger(15, false));
           }
        }
        else
            builder.SetTrigger(new TimeTrigger((uint)ts.TotalMinutes, false));
        BackgroundTaskRegistration result;  
        var bas = (await BackgroundExecutionManager.RequestAccessAsync());
        if (bas == BackgroundAccessStatus.AllowedMayUseActiveRealTimeConnectivity)
            result = builder.Register();
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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