当我的位置靠近大头针 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 (将#修改为@)