2015-05-23 49 views
1

我開發的Windows Phone 8.1 WinRT c#/ Xaml應用程序需要每天早上下載一些json字符串。背景任務在windows phone 8.1的特定時間觸發應用程序

如何讓我的後臺任務在當天的特定時間運行。我應該使用什麼觸發器?

請有人指導我或指點我一些教程。

謝謝

+0

嗨,你有沒有找到你的問題的答案?你願意分享嗎? – belzebu

+0

@belzebu請參考下面的答案。我爲你發佈了這個方法。 –

回答

1

這就是我如何解決我的問題。

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(); 
    } 
相關問題