我需要在Windows Phone 8.1上做一个应用程序,每15分钟在后台任务中更新我的位置。
即使用户在场,任务也必须工作。 我怎样才能做到这一点 ?
我刚刚创build了一个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 } } } }
我假设你得到一个异常值,输入值15。
但是,在windows phone 8.1中,时间触发至少30分钟,windows store应用最少15分钟。