I need to do an app on Windows phone 8.1 that update my location every 15 minutes in a background task.
The task have to work even if the user is present or not. How can I do this ?
I have just created a BackgroundTask
and I register it with a TimeTrigger
but it doesn't work.
This is my register method:
var access = await BackgroundExecutionManager.RequestAccessAsync();
BackgroundTaskBuilder builder = new BackgroundTaskBuilder();
IBackgroundTrigger trigger = new TimeTrigger(15, false);
builder.Name = "BackgroundTask";
builder.SetTrigger(trigger);
builder.TaskEntryPoint = typeof(LocationTask).FullName;
BackgroundTaskRegistration register = builder.Register();
And this is my LocationTask
:
public sealed class LocationTask : IBackgroundTask
{
public async void Run(IBackgroundTaskInstance taskInstance)
{
try
{
Geolocator geolocator = new Geolocator();
Geoposition geoposition = await geolocator.GetGeopositionAsync();
Risorse.lat = Math.Round(geoposition.Coordinate.Point.Position.Latitude, 6);
Risorse.lon = Math.Round(geoposition.Coordinate.Point.Position.Longitude, 6);
DrupalBridge db = new DrupalBridge("http://interventi.computerhalley.it", "/rest", Risorse.utente, Risorse.lat.ToString().Replace(',', '.'), Risorse.lon.ToString().Replace(',', '.'));
db.postCoordinate();
}
catch (Exception ex)
{
if ((uint)ex.HResult == 0x80004004)
{
MessageDialog messaggio = new MessageDialog("GPS disattivato...l'applicazione verrĂ chiusa...\r\nRiavviarla dopo aver attivato la geolocalizzazione");
await messaggio.ShowAsync();
Application.Current.Exit();
//await Windows.System.Launcher.LaunchUriAsync(new Uri("ms - impostazioni - posizione"));
}
else
{
MessageDialog messaggio = new MessageDialog("Errore imprevisto\r\nriavviare l'applicazione...");
await messaggio.ShowAsync();
Application.Current.Exit();
// something else happened acquring the location
}
}
}
}
I assume you are getting an exception with the value 15 entered.
However, in windows phone 8.1 the time trigger is minimum 30 minutes, windows store apps the minimum is 15 minutes.
User contributions licensed under CC BY-SA 3.0