Forum Xamarin.Forms

How to check every time for GPS Connectivity?

HarshitaHarshita INMember ✭✭✭✭

Here is my code for asking user to turn off GPS:
public async void turnOnGps()
{
try
{
MainActivity activity = 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 (Exception ex)
            {

            }
        }

I am using Device.Timer for each 1 second to check the gps connectivity,but the problem is that when gps is turn off and I call this function then it will continiously opening the location popup one by one.so how to solve this?
I want that when The popup showing on the screen then it will not open the second one,if user cancle this popup then I want to show this popup again.

Answers

  • jezhjezh Member, Xamarin Team Xamurai

    You can define a global int variable,and init value:

     int value = 0;
    

    The first time to request the location popup, plus 1:

       if (locationSettingsResult.Status.StatusCode == LocationSettingsStatusCodes.ResolutionRequired)
       {
          if(value<=0){
              locationSettingsResult.Status.StartResolutionForResult(activity, REQUEST_CHECK_SETTINGS);   
           }
          value = value + 1;
       }  
    

    When user cancle this popup, set value = 0;

        protected override async void OnActivityResult (int requestCode, Result resultCode, Intent data)
        {
            switch (requestCode) {
            case  REQUEST_CHECK_SETTINGS:
                switch (resultCode) {
                case Result.Ok:
                   value = 0;//  reset to 0
    
                    break;
                case Result.Canceled:
                    value = 0;//  reset to 0
                    break;
                }
                break;
            }
        }
    
Sign In or Register to comment.