public void OnFailure(Java.Lang.Exception e) { int statusCode = ((ApiException)e).StatusCode; switch (statusCode) { case CommonStatusCodes.ResolutionRequired: Log.Info(Activity.Tag, "Location settings are not satisfied. Attempting to upgrade location settings "); try { ResolvableApiException rae = (ResolvableApiException)e; rae.StartResolutionForResult(Activity, Activity.RequestCheckSettings); } catch (IntentSender.SendIntentException) { Log.Info(Activity.Tag, "PendingIntent unable to execute request."); } break; case LocationSettingsStatusCodes.SettingsChangeUnavailable: string errorMessage = "Location settings are inadequate, and cannot be fixed here. Fix in Settings."; Log.Error(Activity.Tag, errorMessage); Toast.MakeText(Activity, errorMessage, ToastLength.Long).Show(); Activity.mRequestingLocationUpdates = false; break; } Activity.UpdateUi(); }
private void CheckLocationSettings() { LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder() .AddLocationRequest(mLocationRequest); builder.SetAlwaysShow(true); var task = LocationServices.GetSettingsClient(Activity).CheckLocationSettings(builder.Build()); task.AddOnSuccessListener(new OnSuccessListener(t => { mLocationCallback.MyLocation += MLocationCallback_MyLocation; })).AddOnFailureListener(new OnFailureListener(e => { int statusCode = ((ApiException)e).StatusCode; switch (statusCode) { case CommonStatusCodes.ResolutionRequired: try { ResolvableApiException resolvable = (ResolvableApiException)e; StartIntentSenderForResult(resolvable.Resolution.IntentSender, RequestCode, null, 0, 0, 0, null); } catch (SendIntentException) { } catch (ClassCastException) { } break; case LocationSettingsStatusCodes.SettingsChangeUnavailable: 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}"); } } }
private async void OnClickSetMockMode(object sender, EventArgs eventArgs) { string Tag = "SetMockMode"; // Note: To enable the mock function, enable the android.permission.ACCESS_MOCK_LOCATION permission in the AndroidManifest.xml file, // and set the application to the mock location app in the device setting. Task task = fusedLocationProviderClient.SetMockModeAsync(mockFlag); try { await task; if (task.IsCompleted) { log.Info(Tag, $"{Tag} is succeeded"); } else { log.Error(Tag, $"{Tag} failed: {task.Exception.Message}"); } } catch (Exception e) { log.Error(Tag, $"{Tag} exception: {e.Message}"); if (e is ApiException apiException) { int statuesCode = apiException.StatusCode; if (statuesCode == Huawei.Hms.Common.Api.CommonStatusCodes.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, "Unable to start resolution."); } } } } }
public void OnFailure(Java.Lang.Exception e) { LocationLog.Error(TAG, "checkLocationSetting onFailure:" + e.Message); int statusCode = ((ApiException)e).StatusCode; switch (statusCode) { case LocationSettingsStatusCodes.ResolutionRequired: try { //When the startResolutionForResult is invoked, a dialog box is displayed, asking you to open the corresponding permission. ResolvableApiException rae = (ResolvableApiException)e; rae.StartResolutionForResult(callbackActivity, 0); } catch (IntentSender.SendIntentException sie) { LocationLog.Error(TAG, "PendingIntent unable to execute request."); } break; } }
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}"); } } }
protected override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); SetContentView(Resource.Layout.activity_test); _startBtn = FindViewById <Button>(Resource.Id.startBtn); _locationTv = FindViewById <TextView>(Resource.Id.locationTv); _loginBtn = FindViewById <Button>(Resource.Id.loginBtn); _getRecordsBtn = FindViewById <Button>(Resource.Id.getRecordsBtn); _startBtn.Enabled = false; _loginBtn.Enabled = false; if (Build.VERSION.SdkInt >= BuildVersionCodes.Q) { CheckPermission(Android.Manifest.Permission.AccessBackgroundLocation, Android.Manifest.Permission.AccessFineLocation, Android.Manifest.Permission.AccessCoarseLocation, Android.Manifest.Permission.ControlLocationUpdates, Android.Manifest.Permission.Camera, Android.Manifest.Permission.ReadExternalStorage, Android.Manifest.Permission.WriteExternalStorage); } else { CheckPermission(Android.Manifest.Permission.AccessFineLocation, Android.Manifest.Permission.AccessCoarseLocation, Android.Manifest.Permission.ControlLocationUpdates, Android.Manifest.Permission.Camera, Android.Manifest.Permission.ReadExternalStorage, Android.Manifest.Permission.WriteExternalStorage); } _startBtn.Click += delegate { LocationRequest locationRequest = LocationRequest.Create(); locationRequest.SetInterval(10000); locationRequest.SetFastestInterval(5000); locationRequest.SetPriority(LocationRequest.PriorityHighAccuracy); LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder(); builder.AddLocationRequest(locationRequest); SettingsClient client = LocationServices.GetSettingsClient(this); var task = client.CheckLocationSettings(builder.Build()) .AddOnSuccessListener(new SuccessLisenter(async(obj) => { var userIds = new Dictionary <string, string>(); userIds.Add(_userId, _userId); var adding = await ServiceInstances.TripRepository.InsertAsync(new Common.Models.Trip() { Description = "Test trips", UserIds = userIds }); if (adding != null) { _tripId = adding.Id; StartService(_tripId, _userId); } })).AddOnFailureListener(new FailureLisenter((ex) => { if (ex is ResolvableApiException) { try { ResolvableApiException resolvable = (ResolvableApiException)ex; resolvable.StartResolutionForResult(this, 100); } catch (IntentSender.SendIntentException e2x) { // Ignore the error. } } })); }; _loginBtn.Click += async delegate { var authProvider = new FirebaseAuthProvider(new FirebaseConfig("AIzaSyCxjza0PW9fg6y4tPlljkP-iBSwOC0XY6g")); var login = await authProvider.SignInWithEmailAndPasswordAsync("*****@*****.**", "Hello_2020"); if (login != null) { _userId = login.User.LocalId; var firebase = new FirebaseClient( "https://friendloc-98ed3-default-rtdb.firebaseio.com/", new FirebaseOptions { AuthTokenAsyncFactory = () => Task.FromResult(login.FirebaseToken) }); ServiceInstances.TripRepository.Init(firebase); ServiceInstances.UserRepository.Init(firebase); _startBtn.Enabled = true; } }; _getRecordsBtn.Click += async delegate { var res = await ServiceInstances.TripRepository.GetById(_tripId); Console.WriteLine(res.Locations.Values.ToList()[0].CreatedTime); }; }