我正在使用Xam.Plugin.GeoLocator获取当前位置,但是它引发了下面提到的异常:
Theme.AppCompat.Light.DarkActionBar
我的代码在较低的android版本中运行良好,仅在像oreo这样的较高版本中出现问题。
我已经尝试Xamarin form Geolocation task cancelled exception了,但是对我没有用。
{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 }
我的AndroidManifest代码:
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;
}
}
}
}
我已经提供了运行时权限。
<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>