void OnGPSTimerElapsed(object sender, ElapsedEventArgs e) { locationManager.RemoveUpdates(this); this.RunOnUiThread(() => progressDialog.Dismiss()); if (gpsTimer != null && gpsTimer.Enabled) { gpsTimer.Stop(); gpsTimer.Close(); } this.RunOnUiThread(() => { AlertDialog alert = new AlertDialog.Builder(this) .SetPositiveButton("OK", (sender1, EventArgs) => { }) .SetMessage("Unable to get GPS location.") .SetTitle("GPS Location") .Show(); int titleDividerId = Resources.GetIdentifier("titleDivider", "id", "android"); View titleDivider = alert.FindViewById(titleDividerId); if (titleDivider != null) { titleDivider.SetBackgroundColor(Resources.GetColor(Resource.Color.material_blue_grey_800)); } }); }
public void StartGPS() { try { long checkInterval = 0; long minDistance = 0; locationManager = (LocationManager)this.GetSystemService(Activity.LocationService); string locationProvider = null; Criteria criteriaForLocationService = new Criteria { Accuracy = Accuracy.Coarse }; IList <string> acceptableLocationProviders = locationManager.GetProviders(criteriaForLocationService, true); if (acceptableLocationProviders.Any()) { locationProvider = acceptableLocationProviders.First(); locationManager.RequestLocationUpdates(locationProvider, checkInterval, minDistance, this); gpsTimer = new Timer(gpsTimeout); gpsTimer.Elapsed += OnGPSTimerElapsed; gpsTimer.Enabled = true; progressDialog = new ProgressDialog(this); progressDialog.SetTitle("GPS Location"); progressDialog.SetMessage("Getting GPS location..."); progressDialog.SetProgressStyle(ProgressDialogStyle.Spinner); progressDialog.SetCancelable(true); progressDialog.CancelEvent += OnGPSCancel; progressDialog.Show(); int titleDividerId = Resources.GetIdentifier("titleDivider", "id", "android"); View titleDivider = progressDialog.FindViewById(titleDividerId); if (titleDivider != null) { titleDivider.SetBackgroundColor(Resources.GetColor(Resource.Color.material_blue_grey_800)); } Log.Debug("ProviderSearch", "gps requesting updates"); } else { AlertDialog alert = new AlertDialog.Builder(this) .SetPositiveButton("Yes", (sender, EventArgs) => { Intent gpsOptionsIntent = new Intent(Android.Provider.Settings.ActionLocationSourceSettings); StartActivity(gpsOptionsIntent); }) .SetNegativeButton("No", (sender, EventArgs) => { }) .SetMessage("This requires your GPS to be switched on. Switch on GPS and retry.") .SetTitle("Turn on GPS") .Show(); int titleDividerId = Resources.GetIdentifier("titleDivider", "id", "android"); View titleDivider = alert.FindViewById(titleDividerId); if (titleDivider != null) { titleDivider.SetBackgroundColor(Resources.GetColor(Resource.Color.material_blue_grey_800)); } } } catch (System.Exception ex) { } }