Windows Phone 8.1后台位置跟踪

时间:2015-12-17 08:36:18

标签: c# windows background gps background-task

我需要在Windows Phone 8.1上创建一个应用程序,在后台任务中每15分钟更新一次我的位置。

即使用户在场,任务也必须有效。 我怎么能这样做?

我刚刚创建了一个BackgroundTask,我用TimeTrigger注册了它,但它不起作用。

这是我的注册方法:

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();

这是我的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
            }
        }
    }
}

1 个答案:

答案 0 :(得分:1)

我假设您输入的值为15时会出现异常。

然而,在Windows Phone 8.1中,时间触发最少30分钟,Windows应用程序应用最少为15分钟。