Quantcast
Channel: Recent Threads — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 204402

How to wait program till user allows the permission of location service

$
0
0

Hi,
I am creating an application which is required location service. In the android MainActivity.cs I had written the code which display the popup location service.

In the xamarin shared program (App.xaml.cs) I had written the code to get the current location which is called in OnStart function.

Now the program is executing before user allows the permission of location service.

How can I wait to executing the program till user allows the permission.

Following is my code in xamarin shared program (App.xaml.cs)

protected override void OnStart()
        {
            MessagingCenter.Send<App, string>((App)App.Current, "OneMessage", "");
            getGPSLocationCurrent();
        }

public async void getGPSLocationCurrent()
        {
            try
            {
                var lblLat = "";
                var lblLong = "";
                var lblAlt = "";
                var locator = CrossGeolocator.Current;
                locator.DesiredAccuracy = 100;

                var location = await locator.GetPositionAsync(TimeSpan.FromSeconds(10));

                if (location != null)
                {
                    lblLat += location.Latitude.ToString();
                    lblLong += location.Longitude.ToString();
                    lblAlt += location.Altitude.ToString();
                }
                App.gblLatitude = lblLat;
                App.gblLongitude = lblLong;
                App.gblAltitude = lblAlt;
                return;
            }
            catch (FeatureNotSupportedException fnsEx)
            {
                // Handle not supported on device exception  
                // await DisplayAlert("Alert", fnsEx.Message.ToString(), "OK");
                return;
            }
            catch (PermissionException pEx)
            {
                // Handle permission exception  
                // await DisplayAlert("Alert", pEx.Message.ToString(), "OK");
                return;
            }
            catch (Exception ex)
            {
                // Unable to get location  
                //  await DisplayAlert("Alert", ex.Message.ToString(), "OK");
                return;
            }
        }

Viewing all articles
Browse latest Browse all 204402

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>