protected override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); SetContentView(Resource.Layout.main_activity); var toolbar = FindViewById <Toolbar>(Resource.Id.toolbar); SetSupportActionBar(toolbar); mStartUpdatesButton = FindViewById <Button>(Resource.Id.start_updates_button); mStopUpdatesButton = FindViewById <Button>(Resource.Id.stop_updates_button); mLatitudeTextView = FindViewById <TextView>(Resource.Id.latitude_text); mLongitudeTextView = FindViewById <TextView>(Resource.Id.longitude_text); mLastUpdateTimeTextView = FindViewById <TextView>(Resource.Id.last_update_time_text); mStartUpdatesButton.Click += StartUpdatesButtonHandler; mStopUpdatesButton.Click += StopUpdatesButtonHandler; mLatitudeLabel = GetString(Resource.String.latitude_label); mLongitudeLabel = GetString(Resource.String.longitude_label); mLastUpdateTimeLabel = GetString(Resource.String.last_update_time_label); mRequestingLocationUpdates = false; mLastUpdateTime = string.Empty; UpdateValuesFromBundle(savedInstanceState); mFusedLocationClient = LocationServices.GetFusedLocationProviderClient(this); mSettingsClient = LocationServices.GetSettingsClient(this); CreateLocationCallback(); CreateLocationRequest(); BuildLocationSettingsRequest(); }
protected override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); SetContentView(Resource.Layout.activity_huaweilocation); //Button click listeners FindViewById(Resource.Id.location_requestLocationUpdatesWithCallback).SetOnClickListener(this); FindViewById(Resource.Id.location_removeLocationUpdatesWithcallback).SetOnClickListener(this); AddLogFragment(); mFusedLocationProviderClient = LocationServices.GetFusedLocationProviderClient(this); mSettingsClient = LocationServices.GetSettingsClient(this); mLocationRequest = new LocationRequest(); // Sets the interval for location update (unit: Millisecond) mLocationRequest.SetInterval(5000); // Sets the priority mLocationRequest.SetPriority(LocationRequest.PriorityHighAccuracy); if (null == mLocationCallback) { mLocationCallback = new LocationCallbackImpl(); } if (Android.Support.V4.Content.ContextCompat.CheckSelfPermission(this, Manifest.Permission.AccessFineLocation) != Permission.Granted && Android.Support.V4.Content.ContextCompat.CheckSelfPermission(this, Manifest.Permission.AccessCoarseLocation) != Permission.Granted) { string[] strings = { Manifest.Permission.AccessFineLocation, Manifest.Permission.AccessCoarseLocation }; ActivityCompat.RequestPermissions(this, strings, 1); } }
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; } })); }
protected override void OnCreate(Bundle bundle) { base.OnCreate(bundle); SetContentView(Resource.Layout.main); var toolbar = FindViewById <Android.Support.V7.Widget.Toolbar>(Resource.Id.toolbar); if (toolbar != null) { SetSupportActionBar(toolbar); SupportActionBar.SetDisplayHomeAsUpEnabled(false); SupportActionBar.SetHomeButtonEnabled(false); } startButton = FindViewById <Button>(Resource.Id.start_updates_button); startButton.Click += async(sender, args) => await StartLocationUpdatesAsync(); stopButton = FindViewById <Button>(Resource.Id.stop_updates_button); stopButton.Click += (sender, args) => StopLocationUpdates(); latitudeTextView = FindViewById <TextView>(Resource.Id.latitude_text); longitudeTextView = FindViewById <TextView>(Resource.Id.longitude_text); timeTextView = FindViewById <TextView>(Resource.Id.last_update_time_text); fusedLocationClient = LocationServices.GetFusedLocationProviderClient(this); settingsClient = LocationServices.GetSettingsClient(this); if (bundle != null) { var keys = bundle.KeySet(); if (keys.Contains(KeyRequesting)) { requestingUpdates = bundle.GetBoolean(KeyRequesting); } if (keys.Contains(KeyLocation)) { currentLocation = bundle.GetParcelable(KeyLocation) as Location; } if (keys.Contains(KeyLastUpdated)) { lastUpdateTime = bundle.GetString(KeyLastUpdated); } } locationCallback = new MyLocationCallback(); //Create request and set intervals: //Interval: Desired interval for active location updates, it is inexact and you may not receive upates at all if no location servers are available //Fastest: Interval is exact and app will never receive updates faster than this value locationRequest = new LocationRequest() .SetInterval(UpdateInterval) .SetFastestInterval(FastestUpdateInterval) .SetPriority(LocationRequest.PriorityHighAccuracy); locationSettingsRequest = new LocationSettingsRequest.Builder().AddLocationRequest(locationRequest).Build(); UpdateUI(); }
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; } } }
private void InitializeFields() { locationRequest = new LocationRequest(); //Sets the interval for location update (unit: Millisecond) locationRequest.SetInterval(10000); //Sets the priority locationRequest.SetPriority(LocationRequest.PriorityHighAccuracy); fusedLocationProviderClient = LocationServices.GetFusedLocationProviderClient(this); settingsClient = LocationServices.GetSettingsClient(this); }
private void InitializeFields() { locationCallback = new GeoFenceLocationCallback(); locationRequest = new LocationRequest(); locationRequest.SetInterval(5000); locationRequest.SetPriority(LocationRequest.PriorityHighAccuracy); fusedLocationProviderClient = LocationServices.GetFusedLocationProviderClient(this); settingsClient = LocationServices.GetSettingsClient(this); }
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) { 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)); }
protected override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); SetContentView(Resource.Layout.activity_location); startButton = FindViewById <Button>(Resource.Id.btnStartLocationRequest); stopButton = FindViewById <Button>(Resource.Id.btnStopLocationRequest); startButton.Click += StartButton_Click; stopButton.Click += StopButton_Click; act = this; lstView = FindViewById <ListView>(Resource.Id.lstLocations); fusedLocationProviderClient = LocationServices.GetFusedLocationProviderClient(this); settingsClient = LocationServices.GetSettingsClient(this); locationRequest = new LocationRequest() .SetPriority(LocationRequest.PriorityHighAccuracy) .SetNumUpdates(10); locationRequest.SetFastestInterval(30000); locationRequest.SetInterval(30000); locationRequest.SetNeedAddress(true); locationRequest.SetLanguage("en"); locationRequest.SetCountryCode("EN"); }
public async Task DisplayLocationSettingsRequest() { LocationSettingsResponse locationSettingsResponse; var activity = Xamarin.Essentials.Platform.CurrentActivity; try { var locationRequest = LocationRequest.Create(); locationRequest.SetPriority(LocationRequest.PriorityHighAccuracy); locationRequest.SetInterval(10000); locationRequest.SetFastestInterval(5000); 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; } } //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 CommonStatusCodes.Success: // { // //DoStuffWithLocation(); // break; // } // case CommonStatusCodes.ResolutionRequired: // { // try // { // callback.Status.StartResolutionForResult(this, REQUEST_CHECK_SETTINGS); // } // 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; // } // } //}); }
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); }; }
private void InitializeFields() { settingsClient = LocationServices.GetSettingsClient(this); }
private async Task CheckLocationEnabled() { try { LocationRequest highAccuracyRequest = LocationRequest.Create() .SetPriority(LocationRequest.PriorityHighAccuracy) .SetInterval(HIGH_ACC_INTERVAL_IN_MILLISECONDS) .SetFastestInterval(FASTEST_HIGH_ACC_UPDATE_INTERVAL); LocationRequest balancedRequest = LocationRequest.Create() .SetPriority(LocationRequest.PriorityBalancedPowerAccuracy) .SetInterval(BAL_ACC_INTERVAL_IN_MILLISECONDS) .SetFastestInterval(FASTEST_BAL_ACC_UPDATE_INTERVAL); LocationSettingsRequest.Builder locationSettingsRequestBuilder = new LocationSettingsRequest.Builder() .AddLocationRequest(highAccuracyRequest) .AddLocationRequest(balancedRequest) .SetAlwaysShow(true); LocationSettingsResponse locationSettingsResult = await LocationServices.GetSettingsClient(this) .CheckLocationSettingsAsync(locationSettingsRequestBuilder.Build()); if (locationSettingsResult.LocationSettingsStates.IsLocationPresent && locationSettingsResult.LocationSettingsStates.IsLocationUsable) { IsLocationEnabled = true; } else { IsLocationEnabled = false; } } catch (ApiException exception) { if (exception.StatusCode != CommonStatusCodes.ResolutionRequired) { if (exception.StatusCode == LocationSettingsStatusCodes.SettingsChangeUnavailable) { // GetLocationPermissions(Manifest.Permission.AccessFineLocation); } } else { // Location settings are not satisfied. But could be fixed by showing the // user a dialog. try { var resolvable = exception as ResolvableApiException; resolvable?.StartResolutionForResult(this, REQUEST_TURN_ON_LOCATION_ID); } catch (Exception) { // ignored } } } catch (Exception e) { // Crashlytics.Crashlytics.LogException(Throwable.FromException(e)); } }