Example #1
0
        private async void OnClickSetMockMode(object sender, EventArgs eventArgs)
        {
            string Tag = "SetMockMode";
            // Note: To enable the mock function, enable the android.permission.ACCESS_MOCK_LOCATION permission in the AndroidManifest.xml file,
            // and set the application to the mock location app in the device setting.
            Task task = fusedLocationProviderClient.SetMockModeAsync(mockFlag);

            try
            {
                await task;
                if (task.IsCompleted)
                {
                    log.Info(Tag, $"{Tag} is succeeded");
                }
                else
                {
                    log.Error(Tag, $"{Tag} failed: {task.Exception.Message}");
                }
            }
            catch (Exception e)
            {
                log.Error(Tag, $"{Tag} exception: {e.Message}");
                if (e is ApiException apiException)
                {
                    int statuesCode = apiException.StatusCode;
                    if (statuesCode == Huawei.Hms.Common.Api.CommonStatusCodes.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, "Unable to start resolution.");
                        }
                    }
                }
            }
        }