如何解决当我的位置靠近大头针 xamarin
您好,当我的应用程序打开时,我收到一条通知并且它正在运行,但我希望当应用程序在后台时我也收到一条通知。 我试过这个 这是获取位置的功能
public async Task<Location> GetCurrentLocation()
{
var message = new StartLongRunningTaskMessage();
MessagingCenter.Send(message,"StartLongRunningTaskMessage");
var p = new Location();
try
{
var request = new GeolocationRequest(GeolocationAccuracy.Medium,TimeSpan.FromSeconds(10));
cts = new CancellationTokenSource();
var location = await Geolocation.GetLocationAsync(request,cts.Token);
if (location != null)
{
map.MovetoRegion(MapSpan.FromCenterandRadius(new Position(location.Latitude,location.Longitude),distance.FromMeters(100)));
}
return location;
}
catch (FeatureNotSupportedException fnsEx)
{
}
catch (PermissionException pEx)
{
}
catch (Exception ex)
{
}
return p;
}
////////////////////////////
public override StartCommandResult OnStartCommand(Intent intent,StartCommandFlags flags,int startId)
{
_cts = new CancellationTokenSource();
Task.Run( () => {
try
{
//INVOKE THE SHARED CODE
var counter = new Map();
counter.GetCurrentLocation().Wait();
}
catch (System.OperationCanceledException)
{
}
finally
{
if (_cts.IsCancellationRequested)
{
}
}
},_cts.Token);
return StartCommandResult.Sticky;
}
在 MainActivity
void WireUpLongRunningTask()
{
MessagingCenter.Subscribe<StartLongRunningTaskMessage>(this,"StartLongRunningTaskMessage",message => {
var intent = new Intent(this,typeof(LongRunningTaskService));
StartService(intent);
});
}
而且我不知道为什么当我打开应用程序时它不工作,它获取我的位置,然后我将应用程序置于后台,我移动到另一个地方,我打开应用程序,然后它移动到我的位置。我希望当我打开应用程序时,它已经获得了我的新职位。 我希望你能理解我,并为我的英语感到抱歉
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。