简体   繁体   中英

background task in windows phone 8.1

I am using a 3rd party rest api to query data and display it in my app. I have to perform a task like at night 12 approx. it will perform a background task to query data from rest api and update live tile and generate notification. I would like to use only C# only for this task. I don't know what will be best approach to do this task. But I using below code to perform background task to do this which is not working. Not sure why?

BackgroundTaskBuilder taskBuilder = new BackgroundTaskBuilder();
taskBuilder.Name = taskName;
SystemTrigger trigger = new SystemTrigger(SystemTriggerType.InternetAvailable, false);
taskBuilder.SetTrigger(trigger);
taskBuilder.AddCondition(new SystemCondition(SystemConditionType.InternetAvailable));
taskBuilder.TaskEntryPoint = typeof(BackgroundTask.BackgroundTask).FullName;
taskBuilder.Register();

and from background task I am querying data and generating toast notification.

Any help why this code is not working or when this task will fire. Is there any better approach to do above task?

Thanks

Regarding the code you have not working...

For Windows Phone 8.1 unlike Windows 8\\8.1, you are required to call BackgroundExecutionManager.RequestAccessAsync() (search MSDN\\internet) for ANY background task before registering task(s) whereas in Windows this is only required for some tasks. Make sure your code calls this and validate the returned value before registering your background task(s).

Regarding knowing if your task "worked"...

It's a good idea to have the background task implementation run ( IBackgroundTask::Run() ) independent of the trigger\\conditions you've set to ensure it performs without issue by debugging it. See instructions in the following link: http://msdn.microsoft.com/en-US/library/windows/apps/xaml/jj542416.aspx .

Regarding your use of SystemConditionType.InternetAvailable ...

I'm not 100% about this but I'm pretty certain this will be redundant given you already have a SystemTriggerType.InternetAvailable . I don't know of a situation where the trigger would fire but the condition wouldn't be true.

Regarding the requirement you've mentioned...

If I understand your requirement correctly you have different options here:

  1. If your app is a Windows Phone XAML app that need to run based on time, I would recommend either TimeTrigger or MaintenanceTrigger triggers (as opposed to the SystemTrigger). These are both Background Tasks. For general info on Background Tasks and links to the TimeTrigger and MaintenanceTrigger documentation see this MSDN link: http://msdn.microsoft.com/en-US/library/windows/apps/xaml/hh977056.aspx .
  2. If your app is a Windows Phone Silverlight 8.0 app you can use Background Agents, specifically either PeriodicTask or ResourceIntensiveTask . See the links posted by others or search the MSDN\\internet for more info.
  3. If your app is a Windows Phone Silverlight 8.1 app you can use the option in either 1 or 2 above.

I think you should try using PeriodicTask . Also consider the constraints mentioned in the link.

创建一个具有输出类型:Windows Runtime Component的类,并将您的类继承自IBackroundTask,因此如果您从模拟器中使用该类来启动应用程序,则此工作有效,我认为您的用于注册任务的应用程序在模拟器中未激活。

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