public void CheckLocationSetting() { TestTip.Inst.ShowText("RequestLocationUpdatesWithCallback start"); this.requestType = CALLBACK; mFusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(new Context()); mSettingsClient = LocationServices.getSettingsClient(new Context()); mLocationRequest = new LocationRequest(); mLocationRequest.setInterval(5000); mLocationRequest.setPriority(102); // get LocationSettingsRequest LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder(); builder.addLocationRequest(mLocationRequest); LocationSettingsRequest locationSettingsRequest = builder.build(); Task task = mSettingsClient.checkLocationSettings(locationSettingsRequest); task.addOnSuccessListener(new HmsSuccessListener <LocationSettingsResponse>((LocationSettingsResponse setting) => { var status = setting.getLocationSettingsStates(); TestTip.Inst.ShowText($"isBlePresent :{status.isBlePresent()}"); TestTip.Inst.ShowText($"isBleUsable :{status.isBleUsable()}"); TestTip.Inst.ShowText($"isGpsPresent :{status.isGpsPresent()}"); TestTip.Inst.ShowText($"isGpsUsable :{status.isGpsUsable()}"); TestTip.Inst.ShowText($"isLocationPresent :{status.isLocationPresent()}"); TestTip.Inst.ShowText($"isLocationUsable :{status.isLocationUsable()}"); TestTip.Inst.ShowText($"isNetworkLocationPresent :{status.isNetworkLocationPresent()}"); TestTip.Inst.ShowText($"isNetworkLocationUsable :{status.isNetworkLocationUsable()}"); })).addOnFailureListener(new HmsFailureListener((Exception e) => SetSettingsFailuer(e))); }
protected async System.Threading.Tasks.Task CheckLocationSettings() { bool permissionLocationGranted = true; if (Build.VERSION.SdkInt >= BuildVersionCodes.M) { permissionLocationGranted = Application.Context.CheckSelfPermission(Manifest.Permission.AccessFineLocation) == (int)Permission.Granted; if (!permissionLocationGranted) { OnLocationServiceError(LocationErrors.MissingPermission); } } if (permissionLocationGranted) { if (apiClient != null) { //jls LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder(); builder.AddLocationRequest(LocationRequest); var mLocationSettingsRequest = builder.Build(); // BuildLocationSettingsRequest(); var result = await LocationServices.SettingsApi.CheckLocationSettingsAsync( apiClient, mLocationSettingsRequest); await HandleResult(result); } } }
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 void BuildLocationSettingsRequest() { //get the current location settings of a user's device LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder(); builder.AddLocationRequest(mLocationRequest); mLocationSettingsRequest = builder.Build(); }
async Task<bool> CheckLocationAvailability (LocationRequest locationRequest) { // Build a new request with the given location request var locationSettingsRequest = new LocationSettingsRequest.Builder () .AddLocationRequest (locationRequest) .Build (); // Ask the Settings API if we can fulfill this request var locationSettingsResult = await LocationServices.SettingsApi.CheckLocationSettingsAsync (googleApiClient, locationSettingsRequest); // If false, we might be able to resolve it by showing the location settings // to the user and allowing them to change the settings if (!locationSettingsResult.Status.IsSuccess) { if (locationSettingsResult.Status.StatusCode == LocationSettingsStatusCodes.ResolutionRequired) locationSettingsResult.Status.StartResolutionForResult (this, 101); else Toast.MakeText (this, "Location Services Not Available for the given request.", ToastLength.Long).Show (); return false; } return true; }
public void checkLocationSettings() { TestTip.Inst.ShowText("check location settings start"); mFusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(new Context()); mSettingsClient = LocationServices.getSettingsClient(new Context()); mLocationRequest = new LocationRequest(); mLocationRequest.setInterval(5000); mLocationRequest.setPriority(102); LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder(); builder.addLocationRequest(mLocationRequest); LocationSettingsRequest locationSettingsRequest = builder.build(); Task task = mSettingsClient.checkLocationSettings(locationSettingsRequest); task.addOnSuccessListener(new HmsSuccessListener <LocationSettingsResponse>((LocationSettingsResponse locationSettingsResponse) => { var locationSettingsStates = locationSettingsResponse.getLocationSettingsStates(); TestTip.Inst.ShowText($"isBleUsable: {locationSettingsStates.isBleUsable()}"); TestTip.Inst.ShowText($"isBlePresent: {locationSettingsStates.isBlePresent()}"); TestTip.Inst.ShowText($"isGpsPresent: {locationSettingsStates.isGpsPresent()}"); TestTip.Inst.ShowText($"isGpsUsable: {locationSettingsStates.isGpsUsable()}"); TestTip.Inst.ShowText($"isLocationPresent: {locationSettingsStates.isLocationPresent()}"); TestTip.Inst.ShowText($"isLocationUsable: {locationSettingsStates.isLocationUsable()}"); TestTip.Inst.ShowText($"isNetworkLocationPresent: {locationSettingsStates.isNetworkLocationPresent()}"); TestTip.Inst.ShowText($"isNetworkLocationUsable: {locationSettingsStates.isNetworkLocationUsable()}"); })); task.addOnFailureListener(new HmsFailureListener((Exception e) => SetSettingsFailuer(e))); }
public static async Task <bool> CheckForAndTurnOnLocationServices(bool checkOnly = false) { //Debug.WriteLine("### DoingLocationStuff"); if (!checkOnly) { _isLocationOnTask = new TaskCompletionSource <bool>(); } try { var builder = new LocationSettingsRequest.Builder().AddLocationRequest(LocationRequest.Create()) .SetAlwaysShow(true); var response = await LocationServices.GetSettingsClient(MainActivity.Instance) .CheckLocationSettingsAsync(builder.Build()); _isLocationOnTask.SetResult(response.LocationSettingsStates.IsLocationUsable); } catch (Exception e) { if (e is ResolvableApiException exc && !checkOnly) { exc.StartResolutionForResult(MainActivity.Instance, (int)MainActivity.RequestCodes.TurnLocationServicesOnRequestCode); return(await _isLocationOnTask.Task); } Debug.WriteLine("### Location error: " + e); _isLocationOnTask.SetResult(false); } return(await _isLocationOnTask.Task); }
public async Task DisplayLocationSettingsRequest() { var activity = Xamarin.Essentials.Platform.CurrentActivity; LocationSettingsResponse locationSettingsResponse; try { var locationRequest = LocationRequest.Create(); locationRequest.SetPriority(LocationRequest.PriorityHighAccuracy); locationRequest.SetInterval(2000); locationRequest.SetFastestInterval(1250); var locationSettingsRequestBuilder = new LocationSettingsRequest.Builder().AddLocationRequest(locationRequest); locationSettingsRequestBuilder.SetAlwaysShow(false); locationSettingsResponse = await LocationServices.GetSettingsClient(activity).CheckLocationSettingsAsync(locationSettingsRequestBuilder.Build()); } catch (ApiException ex) { switch (ex.StatusCode) { case CommonStatusCodes.ResolutionRequired: var resolvable = (ResolvableApiException)ex; resolvable.StartResolutionForResult(activity, 0x1); break; default: break; } } }
async Task <bool> CheckLocationAvailability(LocationRequest locationRequest) { // Build a new request with the given location request var locationSettingsRequest = new LocationSettingsRequest.Builder() .AddLocationRequest(locationRequest) .Build(); // Ask the Settings API if we can fulfill this request var locationSettingsResult = await LocationServices.SettingsApi.CheckLocationSettingsAsync(googleApiClient, locationSettingsRequest); // If false, we might be able to resolve it by showing the location settings // to the user and allowing them to change the settings if (!locationSettingsResult.Status.IsSuccess) { if (locationSettingsResult.Status.StatusCode == LocationSettingsStatusCodes.ResolutionRequired) { locationSettingsResult.Status.StartResolutionForResult(this, 101); } else { Toast.MakeText(this, "Location Services Not Available for the given request.", ToastLength.Long).Show(); } return(false); } return(true); }
public async void turnOnGps() { try { MainActivity activity = global::Xamarin.Forms.Forms.Context as MainActivity; GoogleApiClient googleApiClient = new GoogleApiClient.Builder(activity).AddApi(LocationServices.API).Build(); googleApiClient.Connect(); LocationRequest locationRequest = LocationRequest.Create(); locationRequest.SetPriority(LocationRequest.PriorityHighAccuracy); locationRequest.SetInterval(10000); locationRequest.SetFastestInterval(10000 / 2); LocationSettingsRequest.Builder locationSettingsRequestBuilder = new LocationSettingsRequest.Builder().AddLocationRequest(locationRequest); locationSettingsRequestBuilder.SetAlwaysShow(false); LocationSettingsResult locationSettingsResult = await LocationServices.SettingsApi.CheckLocationSettingsAsync( googleApiClient, locationSettingsRequestBuilder.Build()); if (locationSettingsResult.Status.StatusCode == LocationSettingsStatusCodes.ResolutionRequired) { locationSettingsResult.Status.StartResolutionForResult(activity, 0); } var result = await LocationServices.SettingsApi.CheckLocationSettingsAsync(googleApiClient, locationSettingsRequestBuilder.Build()); } catch (Exception ex) { } }
//This procedure calls //not sure if im calling this right. //1) is it OK to pass in mainactivity like this? //2) any way to get a callback after they enable? (ie, to reload webview) //3) any way to delay the initial load of the webview until they hit the OK button to load fixity? //https://forums.xamarin.com/discussion/118189/gps-location-enable-in-xamarin-forms //https://stackoverflow.com/questions/33251373/turn-on-location-services-without-navigating-to-settings-page //https://stackoverflow.com/questions/43138788/ask-user-to-turn-on-location/43139125 //https://forums.xamarin.com/discussion/140325/how-to-check-every-time-for-gps-connectivity (code based on this) public async void turnOnGps(MainActivity activity) { try { //MainActivity activity = Xamarin.Forms.Context as MainActivity; GoogleApiClient googleApiClient = new GoogleApiClient.Builder(activity) .AddApi(LocationServices.API).Build(); googleApiClient.Connect(); LocationRequest locationRequest = LocationRequest.Create(); locationRequest.SetPriority(LocationRequest.PriorityHighAccuracy); locationRequest.SetInterval(10000); locationRequest.SetFastestInterval(10000 / 2); LocationSettingsRequest.Builder locationSettingsRequestBuilder = new LocationSettingsRequest.Builder() .AddLocationRequest(locationRequest); locationSettingsRequestBuilder.SetAlwaysShow(false); LocationSettingsResult locationSettingsResult = await LocationServices.SettingsApi.CheckLocationSettingsAsync( googleApiClient, locationSettingsRequestBuilder.Build()); if (locationSettingsResult.Status.StatusCode == LocationSettingsStatusCodes.ResolutionRequired) { locationSettingsResult.Status.StartResolutionForResult(activity, 0); } } catch (Java.Lang.Exception ex) { Xamarin.Forms.DependencyService.Get <IMessage>().LongAlert(ex.Message); //show error } }
public void RequestLocationUpdates(int priority, int requestType) { TestTip.Inst.ShowText("RequestLocationUpdatesWithCallback start"); this.requestType = requestType; mFusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(new Context()); mSettingsClient = LocationServices.getSettingsClient(new Context()); mLocationRequest = new LocationRequest(); mLocationRequest.setInterval(5000); mLocationRequest.setPriority(priority); // get LocationSettingsRequest LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder(); builder.addLocationRequest(mLocationRequest); LocationSettingsRequest locationSettingsRequest = builder.build(); Task task = mSettingsClient.checkLocationSettings(locationSettingsRequest); task.addOnSuccessListener(new LocationSuccessListener((AndroidJavaObject o) => { switch (requestType) { case CALLBACK: SetCallbackSuccess(); break; case INTENT: SetIntentSuccess(); break; case LocationHD: SetHDSuccess(); break; } })); task.addOnFailureListener(new HmsFailureListener((Exception e) => SetSettingsFailuer(e))); }
LocationSettingsRequest.Builder CreateLocationSettingsRequestBuilder() { var builder = new LocationSettingsRequest.Builder(); builder.SetAlwaysShow(!ShowNeverButtonOnUseLocationDialog); builder.AddLocationRequest(CreateLocationRequest()); return(builder); }
public override void OnRequestPermissionsResult(int requestCode, string[] permissions, Android.Content.PM.Permission[] grantResults) { if (CheckCallingOrSelfPermission(Android.Manifest.Permission.AccessFineLocation) == Permission.Granted && CheckCallingOrSelfPermission(Android.Manifest.Permission.AccessCoarseLocation) == Permission.Granted) { LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder().AddLocationRequest(_locRequest); var result = LocationServices.SettingsApi.CheckLocationSettings(_apiClient, builder.Build()); result.SetResultCallback(this); } }
private bool DisplayLocationSettingsRequest() { bool islocationOn = false; var googleApiClient = new GoogleApiClient.Builder(this).AddApi(LocationServices.API).Build(); googleApiClient.Connect(); var locationRequest = LocationRequest.Create(); locationRequest.SetPriority(LocationRequest.PriorityHighAccuracy); locationRequest.SetInterval(10000); locationRequest.SetFastestInterval(10000 / 2); var builder = new LocationSettingsRequest.Builder().AddLocationRequest(locationRequest); builder.SetAlwaysShow(true); var result = LocationServices.SettingsApi.CheckLocationSettings(googleApiClient, builder.Build()); result.SetResultCallback((LocationSettingsResult callback) => { switch (callback.Status.StatusCode) { case LocationSettingsStatusCodes.Success: { islocationOn = true; //DoStuffWithLocation(); break; } case LocationSettingsStatusCodes.ResolutionRequired: { try { // Show the dialog by calling startResolutionForResult(), and check the result // in onActivityResult(). callback.Status.StartResolutionForResult(this, 100); } catch (IntentSender.SendIntentException e) { } break; } default: { // If all else fails, take the user to the android location settings StartActivity(new Intent(Android.Provider.Settings.ActionLocationSourceSettings)); break; } } }); return(islocationOn); }
protected override async Task <bool> EnableDeviceLocationService() { var context = Forms.Context; var activity = (MainActivity)context; var listener = new ActivityResultListener(activity); var googleApiClient = new GoogleApiClient.Builder(activity).AddApi(LocationServices.API).Build(); googleApiClient.Connect(); var locationRequest = LocationRequest.Create(); locationRequest.SetPriority(LocationRequest.PriorityHighAccuracy); locationRequest.SetInterval(10000); locationRequest.SetFastestInterval(10000 / 2); var builder = new LocationSettingsRequest.Builder().AddLocationRequest(locationRequest); builder.SetAlwaysShow(true); var result = LocationServices.SettingsApi.CheckLocationSettings(googleApiClient, builder.Build()); result.SetResultCallback((LocationSettingsResult callback) => { switch (callback.Status.StatusCode) { case LocationSettingsStatusCodes.Success: { break; } case LocationSettingsStatusCodes.ResolutionRequired: { try { // Show the dialog by calling startResolutionForResult(), and check the result // in onActivityResult(). callback.Status.StartResolutionForResult(activity, REQUEST_CHECK_SETTINGS); } catch (IntentSender.SendIntentException e) { } break; } default: { // If all else fails, take the user to the android location settings activity.StartActivity(new Intent(Android.Provider.Settings.ActionLocationSourceSettings)); break; } } }); return(await listener.Task); }
public void RequestLocationUpdatesWithCallback() { LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder(); builder.AddLocationRequest(locationRequest); LocationSettingsRequest locationSettingsRequest = builder.Build(); //checking device settings Task locationSettingsTask = locationSettings.CheckLocationSettings(locationSettingsRequest); locationSettingsTask.AddOnSuccessListener(new OnCallbackSettingsSuccessListener(this, fusedLocationProviderClient, locationRequest, locationCallback)). AddOnFailureListener(new OnCallbackSettingsFailureListener(this)); }
public async void OpenSettings() { LocationManager LM = (LocationManager)Android.App.Application.Context.GetSystemService(Context.LocationService); if (LM.IsProviderEnabled(LocationManager.GpsProvider) == false) { Context ctx = Android.App.Application.Context; p0 = Platform.CurrentActivity; //----------------------------------------------------------------------------------------------------------------- try { GoogleApiClient googleApiClient = new GoogleApiClient.Builder(ctx) .AddApi(LocationServices.API) .Build(); googleApiClient.Connect(); LocationRequest locationRequest = LocationRequest.Create() .SetPriority(LocationRequest.PriorityBalancedPowerAccuracy) .SetInterval(interval) .SetFastestInterval(fastestInterval); LocationSettingsRequest.Builder locationSettingsRequestBuilder = new LocationSettingsRequest.Builder() .AddLocationRequest(locationRequest); locationSettingsRequestBuilder.SetAlwaysShow(false); LocationSettingsResult locationSettingsResult = await LocationServices.SettingsApi.CheckLocationSettingsAsync(googleApiClient, locationSettingsRequestBuilder.Build()); if (locationSettingsResult.Status.StatusCode == LocationSettingsStatusCodes.ResolutionRequired) { locationSettingsResult.Status.StartResolutionForResult(p0, 0); } } catch (Exception exception) { // Log exception } //----------------------------------------------------------------------------------------------------------------- //ctx.StartActivity(new Intent(Android.Provider.Settings.ActionLocationSourceSettings).SetFlags(ActivityFlags.NewTask)); //Application.Context.StartActivity(new Android.Content.Intent(Android.Provider.Settings.ActionLocationSourceSettings)); } else { //this is handled in the PCL } }
private void RequestLocationUpdatesWithIntent() { LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder(); builder.AddLocationRequest(locationRequest); LocationSettingsRequest locationSettingsRequest = builder.Build(); //checking device settings Task locationSettingsTask = settingsClient.CheckLocationSettings(locationSettingsRequest); pendingIntent = CreatePendingIntent(); locationSettingsTask.AddOnSuccessListener(new OnSettingsSuccessListener(this, fusedLocationProviderClient, locationRequest, pendingIntent)). AddOnFailureListener(new OnSettingsFailureListener(this)); }
public async Task CheckAndTurnOnGPS(Page page) { this.page = page; Int64 interval = 1000 * 60 * 1, fastestInterval = 1000 * 50; try { GoogleApiClient googleApiClient = new GoogleApiClient.Builder(MainActivity.Instance) .AddApi(LocationServices.API) .Build(); googleApiClient.Connect(); LocationRequest locationRequest = LocationRequest.Create() .SetPriority(LocationRequest.PriorityBalancedPowerAccuracy) .SetInterval(interval) .SetFastestInterval(fastestInterval); LocationSettingsRequest.Builder locationSettingsRequestBuilder = new LocationSettingsRequest.Builder() .AddLocationRequest(locationRequest); locationSettingsRequestBuilder.SetAlwaysShow(false); LocationSettingsResult locationSettingsResult = await LocationServices.SettingsApi.CheckLocationSettingsAsync( googleApiClient, locationSettingsRequestBuilder.Build()); if (locationSettingsResult.Status.StatusCode == LocationSettingsStatusCodes.ResolutionRequired) { locationSettingsResult.Status.StartResolutionForResult(MainActivity.Instance, 0); } } catch (Exception exception) { Console.Write(exception.Message); } /*(if ((int)Build.VERSION.SdkInt < 23) * { * await MainActivity.Instance.GetLocationAsync(); * return; * } * * await MainActivity.Instance.GetLocationPermissionAsync(this);*/ }
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 StartLocationRequest() { if (activity != null) { LocationRequest request = new LocationRequest() .SetPriority(LocationRequest.PriorityHighAccuracy) .SetFastestInterval(1000) .SetInterval(10000); LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder(); builder.AddLocationRequest(request); LocationServices.GetSettingsClient(activity).CheckLocationSettings(builder.Build()); LocationServices.GetFusedLocationProviderClient(activity).RequestLocationUpdates(request, mCallback, Looper.MyLooper()); } }
public void IsLocationEnabled(Action <bool> returnAction) { lock (Lock) { InitializeGoogleAPI(); if (mGoogleApiClient == null || CheckPermissions() == false) { returnAction(false); return; } mFusedLocationProviderClient = LocationServices.GetFusedLocationProviderClient(Android.App.Application.Context); mGeofencingClient = LocationServices.GetGeofencingClient(Application.Context); mGeofenceList = new List <Android.Gms.Location.IGeofence>(); var locationRequestPriority = LocationRequest.PriorityBalancedPowerAccuracy; switch (CrossGeofence.GeofencePriority) { case GeofencePriority.HighAccuracy: locationRequestPriority = LocationRequest.PriorityHighAccuracy; break; case GeofencePriority.LowAccuracy: locationRequestPriority = LocationRequest.PriorityLowPower; break; case GeofencePriority.LowestAccuracy: locationRequestPriority = LocationRequest.PriorityNoPower; break; } mLocationRequest = new LocationRequest(); mLocationRequest.SetPriority(locationRequestPriority); mLocationRequest.SetInterval(CrossGeofence.LocationUpdatesInterval); mLocationRequest.SetFastestInterval(CrossGeofence.FastestLocationUpdatesInterval); LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder().AddLocationRequest(mLocationRequest); var pendingResult = LocationServices.SettingsApi.CheckLocationSettings(mGoogleApiClient, builder.Build()); pendingResult.SetResultCallback((LocationSettingsResult locationSettingsResult) => { if (locationSettingsResult != null) { returnAction(locationSettingsResult.Status.StatusCode <= CommonStatusCodes.Success); } }); System.Diagnostics.Debug.WriteLine("End of IsLocationEnabled, clients should be created"); } }
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)); }
public LocationService() { GoogleApiClient = new GoogleApiClient.Builder(Android.App.Application.Context) .AddApi(LocationServices.API) .Build(); GoogleApiClient.Connect(); LocationRequest locationRequest = LocationRequest.Create() .SetPriority(LocationRequest.PriorityHighAccuracy) .SetInterval(100000) .SetFastestInterval(100000 / 2); Builder = new LocationSettingsRequest.Builder() .AddLocationRequest(locationRequest) .SetAlwaysShow(true); }
public async Task <bool> TurnOnLocationSettings() { try { Cts = new TaskCompletionSource <bool>(); var client = new GoogleApiClient.Builder(CrossCurrentActivity.Current.Activity).AddApi(LocationServices.API).Build(); client.Connect(); var locationRequest = LocationRequest.Create(); locationRequest.SetPriority(LocationRequest.PriorityBalancedPowerAccuracy); locationRequest.SetInterval(30 * 1000); locationRequest.SetFastestInterval(5 * 1000); var builder = new LocationSettingsRequest.Builder().AddLocationRequest(locationRequest); //builder.SetAlwaysShow(true); //CrossCurrentActivity.Current.ActivityStateChanged += (sender, args) => //{}; var result = await LocationServices.SettingsApi.CheckLocationSettingsAsync(client, builder.Build()); if (result.Status.StatusCode == CommonStatusCodes.Success) { return(true); } if (result.Status.StatusCode == CommonStatusCodes.ResolutionRequired) { result.Status.StartResolutionForResult(CrossCurrentActivity.Current.Activity, 0x1); //REQUEST_CHECK_SETTINGS } else { var settingIntent = new Intent(Android.Provider.Settings.ActionLocationSourceSettings); CrossCurrentActivity.Current.Activity.StartActivity(settingIntent); return(false); } } catch { return(false); } return(await Cts.Task); }
private void RequestLocationUpdatesWithCallback() { try { LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder(); builder.AddLocationRequest(mLocationRequest); LocationSettingsRequest locationSettingsRequest = builder.Build(); // Before requesting location update, invoke checkLocationSettings to check device settings. Task locationSettingsResponseTask = mSettingsClient.CheckLocationSettings(locationSettingsRequest); locationSettingsResponseTask .AddOnSuccessListener(new LocCallSettOnSuccessListenerImpl(mFusedLocationProviderClient, mLocationRequest, mLocationCallback)) .AddOnFailureListener(new LocCallSettOnFailureListenerImpl(this)); } catch (Exception e) { LocationLog.Error(TAG, "requestLocationUpdatesWithCallback exception:" + e.Message); } }
public async static void AskForLocation() { if (!CheckIsGPSEnabled()) { Int64 interval = 1000 * 60 * 1, fastestInterval = 1000 * 50; try { GoogleApiClient googleApiClient = new GoogleApiClient.Builder(MainActivity.AndroidContext) .AddApi(LocationServices.API) .Build(); googleApiClient.Connect(); LocationRequest locationRequest = LocationRequest.Create() .SetPriority(LocationRequest.PriorityBalancedPowerAccuracy) .SetInterval(interval) .SetFastestInterval(fastestInterval); LocationSettingsRequest.Builder locationSettingsRequestBuilder = new LocationSettingsRequest.Builder() .AddLocationRequest(locationRequest); locationSettingsRequestBuilder.SetAlwaysShow(false); LocationSettingsResult locationSettingsResult = await LocationServices.SettingsApi.CheckLocationSettingsAsync( googleApiClient, locationSettingsRequestBuilder.Build()); if (locationSettingsResult.Status.StatusCode == LocationSettingsStatusCodes.ResolutionRequired) { locationSettingsResult.Status.StartResolutionForResult(MainActivity.CurrentActivity, 0); } } catch (Exception) { // Log exception } } }
public async void StartWatcher(int accuracy, int interval, int fastestInterval, LocationCallback callback) { if (this.Running) { return; } this.callback = callback; var locationRequest = new LocationRequest() .SetInterval(interval) .SetFastestInterval(fastestInterval) .SetPriority(accuracy); //location settings dialog GoogleApiClient googleApiClient = new GoogleApiClient.Builder(Application.Context) .AddApi(LocationServices.API) .Build(); googleApiClient.Connect(); LocationSettingsRequest.Builder locationSettingsRequestBuilder = new LocationSettingsRequest.Builder(); locationSettingsRequestBuilder.AddLocationRequest(locationRequest); locationSettingsRequestBuilder.SetAlwaysShow(false); LocationSettingsResult locationSettingsResult = await LocationServices.SettingsApi .CheckLocationSettingsAsync(googleApiClient, locationSettingsRequestBuilder.Build()); if (locationSettingsResult.Status.StatusCode == LocationSettingsStatusCodes.ResolutionRequired) { locationSettingsResult.Status.StartResolutionForResult(activity, 0); } // await client.RequestLocationUpdatesAsync(locationRequest, this.callback).ContinueWith(t => { this.Running = true; }); }
public void PedirGps() { var googleApiClient = new GoogleApiClient.Builder(Context).AddApi(LocationServices.API).Build(); googleApiClient.Connect(); var locationRequest = LocationRequest.Create(); locationRequest.SetPriority(LocationRequest.PriorityHighAccuracy); locationRequest.SetInterval(10000); locationRequest.SetFastestInterval(10000 / 2); var builder = new LocationSettingsRequest.Builder().AddLocationRequest(locationRequest); builder.SetAlwaysShow(true); var result = LocationServices.SettingsApi.CheckLocationSettings(googleApiClient, builder.Build()); result.SetResultCallback(async(LocationSettingsResult callback) => { switch (callback.Status.StatusCode) { case LocationSettingsStatusCodes.Success: { await Utils.PedirPermissaoLocalizacao(); break; } case LocationSettingsStatusCodes.ResolutionRequired: { try { callback.Status.StartResolutionForResult((Activity)Context, Utils.REQUEST_CHECK_SETTINGS); await Utils.PedirPermissaoLocalizacao(); } catch (IntentSender.SendIntentException) { } break; } } }); }
public void IsLocationEnabled(Action <bool> returnAction) { InitializeGoogleAPI(); if (mGoogleApiClient == null) { returnAction(false); return; } var locationRequestPriority = LocationRequest.PriorityBalancedPowerAccuracy; switch (CrossGeofence.GeofencePriority) { case GeofencePriority.HighAccuracy: locationRequestPriority = LocationRequest.PriorityHighAccuracy; break; case GeofencePriority.LowAccuracy: locationRequestPriority = LocationRequest.PriorityLowPower; break; case GeofencePriority.LowestAccuracy: locationRequestPriority = LocationRequest.PriorityNoPower; break; } var locationRequest = new LocationRequest(); locationRequest.SetPriority(locationRequestPriority); locationRequest.SetInterval(CrossGeofence.LocationUpdatesInterval); locationRequest.SetFastestInterval(CrossGeofence.FastestLocationUpdatesInterval); LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder().AddLocationRequest(locationRequest); var pendingResult = LocationServices.SettingsApi.CheckLocationSettings(mGoogleApiClient, builder.Build()); pendingResult.SetResultCallback((LocationSettingsResult locationSettingsResult) => { if (locationSettingsResult != null) { returnAction(locationSettingsResult.Status.StatusCode <= CommonStatusCodes.Success); } }); }
public void IsLocationEnabled(Action<bool> returnAction) { InitializeGoogleAPI(); var locationRequestPriority = LocationRequest.PriorityBalancedPowerAccuracy; switch (CrossGeofence.GeofencePriority) { case GeofencePriority.HighAccuracy: locationRequestPriority = LocationRequest.PriorityHighAccuracy; break; case GeofencePriority.LowAccuracy: locationRequestPriority = LocationRequest.PriorityLowPower; break; case GeofencePriority.LowestAccuracy: locationRequestPriority = LocationRequest.PriorityNoPower; break; } var locationRequest = new LocationRequest(); locationRequest.SetPriority(locationRequestPriority); locationRequest.SetInterval(CrossGeofence.LocationUpdatesInterval); locationRequest.SetFastestInterval(CrossGeofence.FastestLocationUpdatesInterval); LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder().AddLocationRequest(locationRequest); var pendingResult = LocationServices.SettingsApi.CheckLocationSettings(mGoogleApiClient, builder.Build()); pendingResult.SetResultCallback((LocationSettingsResult locationSettingsResult) => { if (locationSettingsResult != null) { returnAction(locationSettingsResult.Status.StatusCode <= CommonStatusCodes.Success); } }); }
private void BuildGoogleLocationSettingsRequest() { LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder(); builder.AddLocationRequest(_googleLocationRequest); _googleLocationSettingsRequest = builder.Build(); }
protected void BuildLocationSettingsRequest () { LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder (); builder.AddLocationRequest (mLocationRequest); mLocationSettingsRequest = builder.Build (); }