async Task StartLocationUpdatesAsync() { if (requestingUpdates) { return; } try { //Check our state here to ensure location is turned on await settingsClient.CheckLocationSettingsAsync(locationSettingsRequest); //Subscribe for location changes locationCallback.LocationUpdated += OnLocationResult; await fusedLocationClient.RequestLocationUpdatesAsync(locationRequest, locationCallback); //If success then we can update the UI requestingUpdates = true; UpdateUI(); } catch (ApiException ex) { switch (ex.StatusCode) { case CommonStatusCodes.ResolutionRequired: try { // Show the dialog by calling startResolutionForResult(), and check the // result in onActivityResult(). var rae = (ResolvableApiException)ex; rae.StartResolutionForResult(this, RequestCheckSettings); } catch (IntentSender.SendIntentException sie) { Debug.WriteLine("PendingIntent unable to execute request."); } break; case LocationSettingsStatusCodes.SettingsChangeUnavailable: var message = "Location settings are inadequate and cannot be changed, please fix in settings."; Toast.MakeText(this, message, ToastLength.Long).Show(); break; } } catch (Exception ex2) { var message = $"Unknown error occured."; Toast.MakeText(this, message, ToastLength.Long).Show(); } }
private async void OnClickCheckSetting(object sender, EventArgs eventArgs) { string Tag = "CheckSetting"; LocationRequest locationRequest = new LocationRequest(); LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder(); builder.AddLocationRequest(locationRequest) .SetAlwaysShow(false) .SetNeedBle(false); var locationSettingsResponseTask = settingsClient.CheckLocationSettingsAsync(builder.Build()); try { await locationSettingsResponseTask; if (locationSettingsResponseTask.IsCompleted && locationSettingsResponseTask.Result != null) { LocationSettingsResponse response = locationSettingsResponseTask.Result; LocationSettingsStates locationSettingsStates = response.LocationSettingsStates; log.Info(Tag, $"CheckLocationSettings completed: {locationSettingsStates.LSSToString()}"); } } catch (Exception e) { if (e is ApiException apiException) { log.Error(Tag, $"CheckLocationSetting Failed. ErrorMessage: {apiException.Message} ErrorCode: {apiException.StatusCode}"); int statuesCode = apiException.StatusCode; if (statuesCode == LocationSettingsStatusCodes.ResolutionRequired) { try { //When the StartResolutionForResult is invoked, a dialog box is displayed, asking you to open the corresponding permission. ResolvableApiException resolvableApiException = (ResolvableApiException)e; resolvableApiException.StartResolutionForResult(this, 0); } catch (IntentSender.SendIntentException sendIntentException) { log.Error(Tag, "PendingIntent unable to execute request."); } } } else { log.Error(Tag, $"CheckLocationSetting Failed: {e.Message}"); } } }
public void IsLocationEnabled(Action <bool> returnAction) { InitializeGoogleAPI(); if (mGeofenceClient == null) { returnAction(false); return; } var locationRequest = GetLocationRequest(); LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder().AddLocationRequest(locationRequest); SettingsClient client = LocationServices.GetSettingsClient(Android.App.Application.Context); var task = client.CheckLocationSettingsAsync(builder.Build()); Task.Run(() => task).ContinueWith(t => LocationSettingsCallback(t, returnAction)); }
private async void OnClickRequestLocationUpdates(object sender, EventArgs eventArgs) { string Tag = "RequestLocationUpdates"; LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder(); builder.AddLocationRequest(locationRequest); LocationSettingsRequest request = builder.Build(); //Before requesting location update, invoke CheckLocationSettings to check device settings. var locationSettingsResponseTask = settingsClient.CheckLocationSettingsAsync(request); try { await locationSettingsResponseTask; if (locationSettingsResponseTask.IsCompleted && locationSettingsResponseTask != null) { LocationSettingsResponse response = locationSettingsResponseTask.Result; var requestLocationUpdatesTask = fusedLocationProviderClient.RequestLocationUpdatesAsync(locationRequest, locationCallback, Looper.MainLooper); try { await requestLocationUpdatesTask; if (requestLocationUpdatesTask.IsCompleted) { log.Info(Tag, "RequestLocationUpdates with callback succeeded."); } else { log.Error(Tag, $"RequestLocationUpdates with callback failed: {requestLocationUpdatesTask.Exception.Message}"); } } catch (Exception e) { log.Error(Tag, $"RequestLocationUpdates with callback failed: {e.Message}"); } } else { var exception = locationSettingsResponseTask.Exception; log.Error(Tag, $"CheckLocationSetting Failed: {exception.Message}"); } } catch (Exception e) { if (e is ApiException apiException) { log.Error(Tag, $"CheckLocationSetting Failed. ErrorMessage: {apiException.Message} ErrorCode: {apiException.StatusCode}"); int statuesCode = apiException.StatusCode; if (statuesCode == LocationSettingsStatusCodes.ResolutionRequired) { try { //When the StartResolutionForResult is invoked, a dialog box is displayed, asking you to open the corresponding permission. ResolvableApiException resolvableApiException = (ResolvableApiException)e; resolvableApiException.StartResolutionForResult(this, 0); } catch (IntentSender.SendIntentException sendIntentException) { log.Error(Tag, "PendingIntent unable to execute request."); } } } else { log.Error(Tag, $"CheckLocationSetting Failed: {e.Message}"); } } }