简体   繁体   中英

StartResolutionForResult always ends up in OnActivityResult with resultCode = Result.Canceled

I am developing a cross-platform mobile app under Xamarin. The app needs to know the user's coordinates. I've tried to use Fused API of Google for best possible accuracy in Android case. After reading some dozens of blog posts about how to handle such a case in Android, I've written code that checks whether the phone settings are sufficient for Fused API (Location enabled and set to High accuracy + WiFi open or WiFi scanning always on). If these prerequisites are not satisfied, then the code starts the default Google resolution for this case and expects the user's reply in OnActivityResult. Despite pressing "OK" as a user, the code in OnActivityResult keeps receiving resultCode = Result.Canceled. However, the settings are changed!!! I've also tried to perform the check once more in OnActivityResult and the result is that the resolution pops-up for a second time, but this time Ok indeed sends a resultCode = Result.Ok signal. I've already read quite a few relative posts in stackoverflow, tried some tricks that were suggested, but none solved the issue. I've already posted the issue on Xamarin forum since early November but nobody has suggested a solution yet. I am testing the app on a ZTE Blade L3 Android 5.0 device. I post the code of the Android Activity at the end of this. I would highly appreciate if somebody could suggest something. Thanks a lot for your time.

[Activity(Label = "TestLocation", 
    Icon = "@drawable/icon", 
    Theme = "@style/MainTheme",
    MainLauncher = true, 
    ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
    public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity,
    GoogleApiClient.IConnectionCallbacks,
    GoogleApiClient.IOnConnectionFailedListener,
    Android.Gms.Location.ILocationListener 
    {
    private GoogleApiClient locationClient;
    private LocationRequest locationRequest;
    private LocationSettingsRequest locationSettingsRequest;
    private const int MIN_TIME_BW_UPDATES = 1000 * 3;
    private const int REQUEST_CHECK_SETTINGS = 9000;

        public async void OnConnected(Bundle connectionHint)
        {
            await CheckFusedApiSettings();
        }

        public void OnConnectionFailed(ConnectionResult result)
        {
            throw new NotImplementedException();
        }

        public void OnConnectionSuspended(int cause)
        {
            throw new NotImplementedException();
        }

        public void OnLocationChanged(Location location)
        {
            throw new NotImplementedException();
        }

        protected override void OnCreate(Bundle bundle)
        {
            TabLayoutResource = Resource.Layout.Tabbar;
            ToolbarResource = Resource.Layout.Toolbar;

            base.OnCreate(bundle);

            InitiateClient();
            InitiateRequests();

            global::Xamarin.Forms.Forms.Init(this, bundle);
            LoadApplication(new App());
        }

        protected override void OnStart()
        {
            base.OnStart();
            locationClient.Connect();
        }

        protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
        {
            base.OnActivityResult(requestCode, resultCode, data);
            if (requestCode == REQUEST_CHECK_SETTINGS)
            {
                switch (resultCode)
                {
                    case Result.Canceled:
                    Toast.MakeText(this, "RESULT CANCEL", ToastLength.Short).Show();
                        break;

                    case Result.Ok:
                        Toast.MakeText(this, "RESULT OK", ToastLength.Short).Show();
                        break;

                    case Result.FirstUser:
                    default:
                        break;
                }
            }
        }

        private void InitiateClient()
        {
            locationClient = new GoogleApiClient.Builder(this, this, this)
                            .AddApi(LocationServices.API)
                            .Build();
            locationClient.Connect();
        }
        private void InitiateRequests()
        {
            locationRequest = new LocationRequest();
            locationRequest.SetPriority(LocationRequest.PriorityHighAccuracy);
            locationRequest.SetFastestInterval(MIN_TIME_BW_UPDATES / 2);
            locationRequest.SetInterval(MIN_TIME_BW_UPDATES);

            LocationSettingsRequest.Builder settingsBuilder = new LocationSettingsRequest.Builder();
            settingsBuilder.AddLocationRequest(locationRequest);
            locationSettingsRequest = settingsBuilder.Build();
        }
        private async Task CheckFusedApiSettings()
        {
            var locationSettingsResult = await LocationServices.SettingsApi.CheckLocationSettingsAsync(locationClient, locationSettingsRequest);

            switch (locationSettingsResult.Status.StatusCode)
            {
                case LocationSettingsStatusCodes.Success:
                    Toast.MakeText(this, "SUCCESS", ToastLength.Short).Show();
                    break;

                case LocationSettingsStatusCodes.ResolutionRequired:
                    try
                    {
                        locationSettingsResult.Status.StartResolutionForResult(this, REQUEST_CHECK_SETTINGS);
                    }
                    catch (Exception e)
                    {
                        Toast.MakeText(this, "CANCEL: " + e.Message, ToastLength.Short).Show();
                    }
                    break;

                default:
                    locationClient.Disconnect();
                    break;
            }
        }
    }

对于对上述问题感兴趣的任何人,请访问: https : //forums.xamarin.com/discussion/comment/275171#Comment_275171在这里,我发表了经过7-8个月的努力得出的结论。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM