[英]Windows phone 8.1 background location tracking
我需要在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
}
}
}
}
我假設您輸入的值15出現異常。
但是,在Windows Phone 8.1中,時間觸發時間最少為30分鍾,Windows存儲應用程序最少為15分鍾。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.