Forum Xamarin.Android

LocationSettings kitkat emulator result canceled

MarkusFMarkusF USMember ✭✭
edited November 2016 in Xamarin.Android

i'm using CheckLocationSettings to check if location is enabled or not. this is working fine on my Galaxy S5. however, on the emulator android kitkat 4.4 the LocationSettingsResult is always 6 ( ResolutionRequired) regardless of whether location is enabled or disabled. also if i answer the location request popup with YES, the OnActivityResult is always RESULT_CANCELED

`protected void buildGoogleApiClient()
{
mGoogleApiClient = new GoogleApiClient.Builder(this)
.AddConnectionCallbacks(this)
.AddOnConnectionFailedListener(this)
.AddApi(LocationServices.API)
.Build();
}

    protected void createLocationRequest()
    {
        mLocationRequest = new LocationRequest();

        mLocationRequest.SetInterval(UPDATE_INTERVAL_IN_MILLISECONDS);

        mLocationRequest.SetFastestInterval(FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS);

        mLocationRequest.SetPriority(LocationRequest.PriorityLowPower);
    }

    protected void buildLocationSettingsRequest()
    {
        LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder();
        builder.AddLocationRequest(mLocationRequest);
        builder.SetAlwaysShow(true);
        mLocationSettingsRequest = builder.Build();
    }

    protected async Task CheckLocationSettings()
    {
        var result = await LocationServices.SettingsApi.CheckLocationSettingsAsync(
            mGoogleApiClient, mLocationSettingsRequest);
        await HandleResult(result);
    }


    public async Task HandleResult(LocationSettingsResult locationSettingsResult)
    {
        var status = locationSettingsResult.Status;
        switch (status.StatusCode)
        {
            case CommonStatusCodes.Success:
                await getlastknownlocation();
                break;
            case CommonStatusCodes.ResolutionRequired:

                try
                {
                    status.StartResolutionForResult(this, REQUEST_CHECK_SETTINGS);
                }
                catch (IntentSender.SendIntentException)
                {

                }
                break;
            case LocationSettingsStatusCodes.SettingsChangeUnavailable:
                break;
        }
    }


            protected override async void OnActivityResult(int requestCode, Result resultCode, Intent data)
    {
        switch (requestCode)
        {
            case REQUEST_CHECK_SETTINGS:
                switch (resultCode)
                {
                    case Result.Ok:
                        await getlastknownlocation();
                        break;
                    case Result.Canceled:
                        break;
                }
                break;
        }
    }`

UPDATE
same behavior on android 5.0 emulator

Answers

  • MarkusFMarkusF USMember ✭✭

    No one? maybe someone can try my code on an emulator too.

    Thanks

  • elefpelefp GRMember ✭✭
    edited November 2016

    Dear Markus,

    I've faced similar problems with you. First of all let me point out that I've used LocationSettingsStatusCodes instead of CommonStatusCodes. I do not know whether this has any significance, but I've found the use of LocationSettingsStatusCodes in most similar implementations online.

    You are getting ResolutionRequired constantly because you are lacking one of the prerequisites for Google Fused API to work. These are:
    1. Open Data Connection
    2. Activated Location with High Accuracy setting chosen
    3. Either open WiFi connection or "Scanning always available" setting ON under Settings->WiFi->Advanced

    If my perception is correct, Google receives the coordinates of all open WiFi stations in proximity to your device and applies an algorithm that gives a guess for your location. It is very accurate indeed! Without point 3 however, you will always get ResolutionRequired. I spent 2 weeks to realize that.

    As far as the other issue is concerned, I keep getting RESULT_CANCELED as well despite pressing Ok!!! And this is driving me crazy, because the settings are changed in the background. I've even tested it with Location totally deactivated and the application does activate it, sets its level to High Accuracy and sets "Scanning always available" setting ON! However, my code cannot realize that since the return value is RESULT_CANCELED. I hope somebody else reads the post and helps us somehow. I've searched a lot but have not got an answer yet.

    UPDATE: Forget prerequisite 1. It's not necessary.

Sign In or Register to comment.