protected override void OnActivityResult(int requestCode, [Android.Runtime.GeneratedEnum] Result resultCode, Intent data) { string Tag = "CheckSettingActivity.OnActivityResult"; base.OnActivityResult(requestCode, resultCode, data); if (data == null) { return; } LocationSettingsStates locationSettingsStates = LocationSettingsStates.FromIntent(data); log.Info(Tag, $"CheckLocationSettings completed: {locationSettingsStates.LSSToString()}"); switch (requestCode) { // Check for the integer request code originally supplied to StartResolutionForResult(). case 0: switch (resultCode) { case Result.Ok: log.Info(Tag, "User agreed to make required location settings changes."); // Nothing to do. StartLocationupdates() gets called in OnResume again. break; case Result.Canceled: log.Info(Tag, "User chose not to make required location settings changes."); break; } break; } }
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}"); } } }