Xamarin.Android-如何在最小化应用或锁定屏幕的情况下继续操作

时间:2018-07-24 15:34:06

标签: asynchronous xamarin xamarin.android

我创建了一个Xamarin应用程序,该应用程序每两分钟检查一次用户当前位置。我的问题是,当我最小化应用程序或屏幕锁定时,它不起作用。有没有办法让我在最小化或锁定手机的情况下继续该过程?

此功能检查用户是否在目标位置。

    public bool atLocation(decimal currentLat, decimal currentLong, decimal destLat, decimal destLong)
    {
        decimal GPSLatitudePadding = 0.001M;
        decimal GPSLongitudePadding = 0.001M;
        var totalLowerLatitude = Convert.ToDecimal((destLat - GPSLatitudePadding).ToString("#.####"));
        var totalLowerLongitude = Convert.ToDecimal((destLong + GPSLongitudePadding).ToString("#.####"));
        var totalUpperLatitude = Convert.ToDecimal((destLat + GPSLatitudePadding).ToString("#.####"));
        var totalUpperLongitude = Convert.ToDecimal((destLong - GPSLongitudePadding).ToString("#.####"));

        if ((Convert.ToDecimal(destLat) >= totalLowerLatitude) && 
        (Convert.ToDecimal(destLat) <= totalUpperLatitude) && 
        (Convert.ToDecimal(destLong) <= totalLowerLongitude) && 
        (Convert.ToDecimal(destLong) >= totalUpperLongitude))
        {
            return true;
        }

        return false;
    }

如果用户在目的地2分钟2分钟,则过程结束。在这里您可以开始新过程或更新数据库。

    int intNumberOfTimesFoundAtLocation = 0;
    void OnLocationResult(object sender, Android.Locations.Location location)
    {
        decimal currentLatitude =  Convert.ToDecimal(location.Latitude); 
        decimal currentLongitude = Convert.ToDecimal(location.Longitude); 
        decimal destLatitude = Convert.ToDecimal(this.dblDestLatitude); 
        decimal destLongitude = Convert.ToDecimal(this.dblDestLongitude); 
        var atLocation = locationQ.atLocation(currentLatitude, currentLongitude, destLatitude, destLongitude);

        if (ayLocation == true)
        {
            intNumberOfTimesFoundAtLocation = intNumberOfTimesFoundAtLocation + 1;
            if(intNumberOfTimesFoundAtLocation == 2)
            { 
                client.RemoveLocationUpdates(locationCallback);

            }
        }
    }

我调用此函数开始该过程

    MyLocationCallback locationCallback;
    FusedLocationProviderClient client;

    public async void StartLocationUpdatesAsync()
    {
        // Create a callback that will get the location updates
        if (locationCallback == null)
        {
            locationCallback = new MyLocationCallback();
            locationCallback.LocationUpdated += OnLocationResult;
        }

        // Get the current client
        if (client == null)
            client = LocationServices.GetFusedLocationProviderClient(this);

        try
        {
            locationRequest = new LocationRequest()
                                      .SetInterval(120000)
                                      .SetFastestInterval(120000)                                          
                           .SetPriority(LocationRequest.PriorityHighAccuracy);

            await client.RequestLocationUpdatesAsync(locationRequest, locationCallback);
        }
        catch (Exception ex)
        {
            //Handle exception here if failed to register
        }
    }

}

class MyLocationCallback : LocationCallback
{
    public EventHandler<Android.Locations.Location> LocationUpdated;
    public override void OnLocationResult(LocationResult result)
    {
        base.OnLocationResult(result);
        LocationUpdated?.Invoke(this, result.LastLocation);
    }
}

1 个答案:

答案 0 :(得分:0)

  • 创建前台服务。
  • 内部服务调用以下代码以开始请求更新。

    locationLogger = new LocationLogger();
    locationLogger.LogInterval = _currentInterval;
    locationLogger.StopUpdates();
    locationLogger.StartRequestionUpdates();
    

检查here中的代码

让我知道是否需要更多帮助。