I have a rather simple question.
We use BackgroundAgents( Periodic agents ) to perform some background tasks which require WiFi(Internet) connectivity in order to perform an HttpWebRequest. As mention in the second reference HttpWebRequest is supported but the problem is that if the Windows Phone is locked or idle for more than 1 minute the WiFi is disabled.
I have two important questions based on what I have read and tried until know:
Thanks.
Sample Code:
protected override void OnInvoke(ScheduledTask task)
{
ScheduledActionService.LaunchForTest(task.Name, TimeSpan.FromSeconds(120));
MakeHttpRequest("test");
}
private void MakeHttpRequest(string position)
{
if (position != null)
{
var request = (HttpWebRequest)WebRequest.Create(
new Uri("http://mydomain.com/Testing/Details/"+position));
request.BeginGetResponse(r =>
{
var httpRequest = (HttpWebRequest)r.AsyncState;
var httpResponse = (HttpWebResponse)httpRequest.EndGetResponse(r);
using (var reader = new StreamReader(httpResponse.GetResponseStream()))
{
var response = reader.ReadToEnd();
Deployment.Current.Dispatcher.BeginInvoke(new Action(() =>
{
}));
}
}, request);
}
this.NotifyComplete();
}
PS: Please keep in mind that when I run this code while the application is connected to the computer using a USB cable everything runs fine. That's why I believe its a problem with the background worker which does not wake the phone + WiFi in order to perform tha HttpWebRequest.
It is my experience with using the phone on a day-to-day basis that Windows Phone does not reenable wifi after it locks unless the phone is plugged in and charging. It also allows data transfers that were already started to complete before disabling the wifi.
However, even if the phone tried to reconnect periodically, some wifi providers redirect to an authentication page for authentication every time the user reconnects and Windows Phone does not handle that situation.
In reality, there's no guarantee that you have data services available as the user walks around with the phone, so handling the issue should be part of a normal path for the application code.