0

I am using Xam.Plugin.GeoLocator for getting the current Location but it is throwing exception mentioned below:

{System.Threading.Tasks.TaskCanceledException: A task was canceled.
  at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) [0x00026] in <43dbbdc147f2482093d8409abb04c233>:0 
  at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) [0x00028] in <43dbbdc147f2482093d8409abb04c233>:0 
  at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) [0x00008] in <43dbbdc147f2482093d8409abb04c233>:0 
  at System.Runtime.CompilerServices.TaskAwaiter`1[TResult].GetResult () [0x00000] in <43dbbdc147f2482093d8409abb04c233>:0 
  at Plugin.Geolocator.GeolocatorImplementation+<GetPositionAsync>d__41.MoveNext () [0x004d3] in <e6cb69436f5b455fa0a66dc46ca4b792>:0 
--- End of stack trace from previous location where exception was thrown ---
  at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) [0x0001a] in <43dbbdc147f2482093d8409abb04c233>:0 
  at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) [0x00028] in <43dbbdc147f2482093d8409abb04c233>:0 
  at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) [0x00008] in <43dbbdc147f2482093d8409abb04c233>:0 
  at System.Runtime.CompilerServices.TaskAwaiter`1[TResult].GetResult () [0x00000] in <43dbbdc147f2482093d8409abb04c233>:0 
  at MyApp.Droid.GetCurrentLocation+<SetLocation>d__2.MoveNext () [0x00089] in .cs:58 }

My code is working fine in lower android version, It is only creating issues in higher version like oreo.

I have already tried Xamarin form Geolocation task cancelled exception this but it did not work for me.

    public async static void SetLocation()
    {
        var locator = CrossGeolocator.Current;
        locator.DesiredAccuracy = 50;
        if (locator.IsGeolocationAvailable && locator.IsGeolocationEnabled)
        {
            try
            {
                var position = await locator.GetPositionAsync(TimeSpan.FromMilliseconds(1000));
                var Latitude = position.Latitude;
                var Longitude = position.Longitude;
                currentLocation = Latitude + "," + Longitude;
            }
            catch (Exception ex)
            {
                return;
            }
        }
    }
}

my code of AndroidManifest:

 <uses-sdk android:minSdkVersion="15" android:targetSdkVersion="22" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />
 <uses-permission android:name="android.permission.READ_INTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_USER_DICTIONARY" />
<uses-permission android:name="android.permission.WRITE_USER_DICTIONARY" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.FLASHLIGHT" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
 <uses-feature android:name="android.hardware.location.gps" />
 <uses-feature android:name="android.hardware.location.network" />

<application android:label="IRMS">
    <provider android:name="android.support.v4.content.FileProvider"
         android:authorities="com.myapp.test.fileprovider"
         android:exported="false"
         android:grantUriPermissions="true">
     <meta-data android:name="android.support.FILE_PROVIDER_PATHS"
     android:resource="@xml/file_paths"></meta-data>
   </provider>

 </application>

I have already provided the runtime permission.

         async Task TryGetLocationAsync()
    {
        if ((int)Build.VERSION.SdkInt < 23)
        {
            await GetLocationAsync();
            return;
        }

        await GetLocationPermissionAsync();
    }

    readonly string[] PermissionsLocation =
   {
    Manifest.Permission.AccessCoarseLocation,
    Manifest.Permission.AccessFineLocation
   };

    const int RequestLocationId = 0;

    async Task GetLocationPermissionAsync()
    {
        //Check to see if any permission in our group is available, if one, then all are
        const string permission = Manifest.Permission.AccessFineLocation;
        if (CheckSelfPermission(permission) == (int)Permission.Granted)
        {
            await GetLocationAsync();
            return;
        }

        //need to request permission
        if (ShouldShowRequestPermissionRationale(permission))
        {
            Android.Content.Context c = Forms.Context;
            new Android.App.AlertDialog.Builder(c)
                     .SetTitle("Location access is required.")
              .SetMessage("Show settings to enable?")
              .SetPositiveButton(
                 "OK", (sender, e) =>
                 {
                     RequestPermissions(PermissionsLocation, RequestLocationId);
                 })
             .SetNegativeButton("Cancel", (sender, e) =>
             {
                 (sender as Android.App.AlertDialog).Hide();
             })
             .Show();
            return;
        }
        //Finally request permissions with the list of permissions and Id
        RequestPermissions(PermissionsLocation, RequestLocationId);
    }
Mannu
  • 209
  • 5
  • 18
  • Runtime permissions are required in the newer API levels (the user must be visually prompted and than accept the permissions), see the linked answer – SushiHangover Jan 15 '19 at 06:30
  • @SushiHangover if there is any issue you can find in my runtime permission please provide the solution for it. – Mannu Jan 15 '19 at 07:15
  • What is the full exception with InnerException/Stacktrace – SushiHangover Jan 15 '19 at 07:17
  • modified in the question – Mannu Jan 15 '19 at 07:23
  • InnerException? Also it certainly seems like the permission is not granted, 2 things: review `logcat` for OS errors and check and manually enable location permissions in the System App Setting on the device/emulator and retry the app and check for the same error – SushiHangover Jan 15 '19 at 07:28
  • In the App settings, location permission is granted for the app. – Mannu Jan 15 '19 at 07:33
  • Do you have any alternate solution for getting the lat long of the current location? – Mannu Jan 15 '19 at 07:34

0 Answers0