Windows phone 8.1后台位置跟踪
本文关键字:位置 跟踪 后台 phone Windows | 更新日期: 2023-09-27 18:27:05
我需要在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分钟。