简体   繁体   中英

Windows phone 8.1 background location tracking

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.

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