Forum Xamarin.Android

Android background location updates for particular time intervals not working with all devices.

Hi guys,

I'm developing an application which sends the device location status to server for every minute(it may change as per configuration). I'm using Geolocator plugin to get location updates from mobile device. This is working fine with few devices, but many other devices are not updating the device location for every minute, the status update time is keep on changing and in some cases I'm getting update after one hour. Please help me to resolve this issue. Please find the GeoLocator location updates code below.

public class LocationService : Service
{
protected const string TAG = "location-updates-service";
IGeolocator locator;
public event EventHandler LocationChanged = delegate { };
public event EventHandler StatusDelayed = delegate { };
IBinder binder;

    public override void OnCreate()
    {
        locator = CrossGeolocator.Current;
        locator.PositionChanged += OnLocationChanged;
        locator.PositionError += PositionError;
        base.OnCreate();
    }

    private void PositionError(object sender, PositionErrorEventArgs e)
    {
        Log.Info(TAG, "Position Error");
    }

    // This gets called when StartService is called in our App class
    public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId)
    {
        return StartCommandResult.Sticky;
    }

    public override IBinder OnBind(Intent intent)
    {
        binder = new LocationServiceBinder(this);
        return binder;
    }

    public void StartLocationUpdates()
    {
        if (!locator.IsListening)
        {
            locator.AllowsBackgroundUpdates = true;
            locator.DesiredAccuracy = 10;
            locator.StartListeningAsync(60 * 1000, 10, false);
        }
    }

    private void OnLocationChanged(object sender, PositionEventArgs e)
    {
        this.LocationChanged(this, new PositionEventArgs(e.Position));
    }

    public void StopLocationUpdates()
    {
        if (locator.IsListening)
            locator.StopListeningAsync();
    }
}
Sign In or Register to comment.