public void OnResume()
        {
            if (this.locationManager != null && this.locationListener != null)
            {
                this.gpsProviderEnabled     = this.locationManager.IsProviderEnabled(LocationManager.GpsProvider);
                this.networkProviderEnabled = this.locationManager.IsProviderEnabled(LocationManager.NetworkProvider);

                var currentTimeMillis = (DateTime.Now - new DateTime(1970, 1, 1)).TotalMilliseconds;

                if (gpsProviderEnabled)
                {
                    var lastKnownGpsLocation = locationManager.GetLastKnownLocation(LocationManager.GpsProvider);

                    if (lastKnownGpsLocation != null && lastKnownGpsLocation.Time > currentTimeMillis - LOCATION_OUTDATED_WHEN_OLDER_MS)
                    {
                        locationListener.OnLocationChanged(lastKnownGpsLocation);
                    }

                    if (locationManager.GetProvider(LocationManager.GpsProvider) != null)
                    {
                        locationManager.RequestLocationUpdates(LocationManager.GpsProvider,
                                                               LOCATION_UPDATE_MIN_TIME_GPS, LOCATION_UPDATE_DISTANCE_GPS, this.locationListener);
                    }
                }

                if (networkProviderEnabled)
                {
                    var lastKnownNWLocation = this.locationManager.GetLastKnownLocation(LocationManager.NetworkProvider);

                    if (lastKnownNWLocation != null && lastKnownNWLocation.Time > currentTimeMillis - LOCATION_OUTDATED_WHEN_OLDER_MS)
                    {
                        locationListener.OnLocationChanged(lastKnownNWLocation);
                    }

                    if (locationManager.GetProvider(LocationManager.NetworkProvider) != null)
                    {
                        locationManager.RequestLocationUpdates(LocationManager.NetworkProvider,
                                                               LOCATION_UPDATE_MIN_TIME_NW, LOCATION_UPDATE_DISTANCE_NW, this.locationListener);
                    }
                }

                if (!gpsProviderEnabled && !networkProviderEnabled)
                {
                    Toast.MakeText(context, "Please enable GPS and Network Positioning in your Settings", ToastLength.Short).Show();
                }
            }
        }
Esempio n. 2
0
        public bool Start()
        {
            if (ContextCompat.CheckSelfPermission(context, Manifest.Permission.AccessFineLocation) != Android.Content.PM.Permission.Granted)
            {
                Log.Error("LocationProvider", context.GetString(Resource.String.permissions_denied) + " Manifest.Permission.AccessFineLocation");
                return(false);
            }

            var gpsProviderEnabled     = locationManager?.IsProviderEnabled(LocationManager.GpsProvider) ?? false;
            var networkProviderEnabled = locationManager?.IsProviderEnabled(LocationManager.NetworkProvider) ?? false;

            var providers = new List <string>();

            if (gpsProviderEnabled)
            {
                providers.Add(LocationManager.GpsProvider);
            }
            if (networkProviderEnabled)
            {
                providers.Add(LocationManager.NetworkProvider);
            }

            foreach (var provider in providers)
            {
                var lastKnownLocation = locationManager.GetLastKnownLocation(provider);
                if (lastKnownLocation != null && lastKnownLocation.Time > Java.Lang.JavaSystem.CurrentTimeMillis() - LocationOutdatedWhenOlderMs)
                {
                    listener.OnLocationChanged(lastKnownLocation);
                }
                if (locationManager.GetProvider(provider) != null)
                {
                    locationManager.RequestLocationUpdates(provider, LocationUpdateMinTime, LocationUpdateDistance, listener);
                }
            }

            if (!gpsProviderEnabled && !networkProviderEnabled)
            {
                return(false);
            }
            return(true);
        }