jamesmontemagno / Xamarin.Plugins

Cross-platform Native API Access from Shared Code!
MIT License
1.3k stars 380 forks source link

Xam.Plugin.Geolocator Exception - A task was canceled. #318

Closed shuaihuadu closed 8 years ago

shuaihuadu commented 8 years ago

Please take a moment to fill out the following (change to preview to check or place x in []) and remove all unused areas

This is a

Which plugin does this impact:

Version Number of Plugin:Xam.Plugin.Geolocator.3.0.4 Device Tested On:Android 4.4 Simulator Tested On:Android 4.4

Expected Behavior

Get current location.

Actual Behavior

Get an error : A task was canceled.

Steps to reproduce the Behavior

Code: var locator = CrossGeolocator.Current; if (locator.IsGeolocationAvailable && locator.IsGeolocationEnabled) { locator.DesiredAccuracy = 50; var position = await locator.GetPositionAsync(timeoutMilliseconds: 10000); CurrentLocation = new LocationModel { Name = Constants.CURRENT_LOCATION, Latitude = position.Latitude, Longitude = position.Longitude }; }

Error Message:

A task was canceled. at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) [0x0002d] in /Users/builder/data/lanes/3053/a94a03b5/source/mono/external/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:193 at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) [0x0002e] in /Users/builder/data/lanes/3053/a94a03b5/source/mono/external/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:170 at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) [0x0000b] in /Users/builder/data/lanes/3053/a94a03b5/source/mono/external/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:142 at System.Runtime.CompilerServices.ConfiguredTaskAwaitable 1+ConfiguredTaskAwaiter[TResult].GetResult () [0x00000] in /Users/builder/data/lanes/3053/a94a03b5/source/mono/external/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:580 at Plugin.Geolocator.GeolocatorImplementation+<GetPositionAsync>d__27.MoveNext () [0x00597] in C:\projects\xamarin-plugins\Geolocator\Geolocator\Geolocator.Plugin.Android\GeolocatorImplementation.cs:175 --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in /Users/builder/data/lanes/3053/a94a03b5/source/mono/external/referencesource/mscorlib/system/runtime/exceptionservices/exceptionservicescommon.cs:143 at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) [0x00027] in /Users/builder/data/lanes/3053/a94a03b5/source/mono/external/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:190 at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) [0x0002e] in /Users/builder/data/lanes/3053/a94a03b5/source/mono/external/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:170 at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) [0x0000b] in /Users/builder/data/lanes/3053/a94a03b5/source/mono/external/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:142 at System.Runtime.CompilerServices.TaskAwaiter 1[TResult].GetResult () [0x00000] in /Users/builder/data/lanes/3053/a94a03b5/source/mono/external/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:372

jamesmontemagno commented 8 years ago

It timed out, you can increase your timeout and make sure to wrap in a try/catch statement.

shuaihuadu commented 8 years ago

@jamesmontemagno Even if I set for 30 minutes, it's still threw an exception.

meluamarilla commented 8 years ago

Same issue here! Help!

ghost commented 8 years ago

Are you checking on actual device or simulator? It throws this exception only on simulator. On an actual device its working fine.

jamesmontemagno commented 8 years ago

I haven not had any issues on Visual Studio emulators for Android, Xamarin Android player, or google emulators. Make sure the emulator support it.

lylxiaole commented 7 years ago

thanks jamesmontemagno,,you make a great plugin......but i met the same issue on my phone.....but i can get my position while use google map.

ghost commented 7 years ago

I have the same problem, it was working for a pair of hours. but now I can't make it work.

Plese helpppp..

altondevir commented 7 years ago

Had the same problem here. I thought some devices were ok, some devices not ok. So I restarted the device that was not OK to my surprise then it worked. I ignore what is going on though

angelieri commented 7 years ago

I have same issue, even in physical device. Some times works, some times not. The most important is that I'm not able to catch the error and continue the program, it just crash the app (I'm new on xamarin and mobile development).

jdamasceno commented 7 years ago

Same problem on the emulator...

giamba61 commented 7 years ago

geolocator crash everytime on android 5.1.1.

gustblima commented 7 years ago

Same problem here. Any solution found yet?

jamesmontemagno commented 7 years ago

If you use an emulator you have to click the send location in the settings each time of the google emulator. It doesn't cache the actual location.

ak47akshaykulkarni commented 7 years ago

Hey James, your efforts in creating plugins are really of help. This issue might not be from the plugin nor from Xamarin but I found this android issue. This issue is open since 2013, and there was no solid solution for the same here is the link: "onLocationChanged" is not called(LocationManager using Network Provider) Still if anyone comes up with some work around for the same, it would be great, cause one of our testing teams device is trapped under this situation 😫 .

trilm208 commented 6 years ago

Same problem here. Version 5.0.0-beta124

Sivamuralidhar commented 5 years ago

same problem here. did anyone find solution please post here.

mrtcofficial commented 5 years ago
  <uses-feature android:name="android.hardware.location.gps" />
  <uses-feature android:name="android.hardware.location.network" />

Adding this in my mainfest.xml and increase timeout like 30 seconds solved my problem

DanielEnid commented 5 years ago

I banged my head for a while on this issue. To solve this, catch the taskCanceledException. When you get this, ask the user to open Google Map, to locate the device then to come back in the app.

GaboMendez commented 4 years ago

@DanielEnid this worked for me, i only opened google maps and go back to the app and everything has worked fine