Help Agents in Windows 8.1 Phone (Silverlight)
I am following this link for implementing ScheduledAgent in WP 8.1 Silverlight.
Steps: -
Edited WMAppManifest.xaml
:
<Tasks>
<DefaultTask Name="_default" NavigationPage="/View/StartPage.xaml" />
<ExtendedTask Name="BackgroundTask">
<BackgroundServiceAgent Specifier="ScheduledTaskAgent" Name="ScheduledTaskAgent2" Source="ScheduledTaskAgent2" Type="ScheduledTaskAgent2.ScheduledAgent" />
</ExtendedTask>
</Tasks>
Added new project ScheduledAgent with target version 8.1 .:
Now my class is ScheduledAgent
#define DEBUG_AGENT
using System;
using System.Diagnostics;
using System.Windows;
using Microsoft.Phone.Scheduler;
using Microsoft.Phone.Shell;
namespace ScheduledTaskAgent2
{
public class ScheduledAgent : ScheduledTaskAgent
{
protected override void OnInvoke(ScheduledTask task)
{
#if DEBUG_AGENT
ScheduledActionService.LaunchForTest(task.Name, TimeSpan.FromSeconds(60));
#endif
NotifyComplete();
}
}
}
My code to run the agent
public const string PeriodicTaskName = "ScheduledTaskAgent2";
private PeriodicTask _periodicTask;
private void StartPeriodicAgent()
{
_isPeriodicTaskStarted = true;
_periodicTask = ScheduledActionService.Find(PeriodicTaskName) as PeriodicTask;
if (_periodicTask != null)
{
RemoveAgent(PeriodicTaskName);
}
_periodicTask = new PeriodicTask(PeriodicTaskName) {Description = "periodic task."};
try
{
ScheduledActionService.Add(_periodicTask);
#if(DEBUG_AGENT)
ScheduledActionService.LaunchForTest(PeriodicTaskName, TimeSpan.FromSeconds(60));
#endif
}
catch (Exception exception){ }
}
private static void RemoveAgent(string name)
{
try
{
ScheduledActionService.Remove(name);
}
catch (Exception){}
}
Now this is all I tried for the background agent. This does not call the method OnInvoke()
(at least in debug mode)
Note . I also added the Reference to ScheduledTaskAgent2 project.
Has anyone implemented ScheduleAgent in WP 8.1 (Silverlight)
Is it supported at all?
source to share
I got the solution This is a completely working solution, just copy it. Can't get this from the documentation directly. Just add this extension to your Package.appxmanifest
file. you can open it with option right click => viewcode
.
<Extension Category="windows.backgroundTasks" EntryPoint="AgHost.BackgroundTask">
<BackgroundTasks>
<Task Type="systemEvent" />
<Task Type="timer"/>
</BackgroundTasks>
</Extension>
source to share