Xamarin Forms Android Pie Not Getting Current Location in App Background Sleep Mode

房东的猫 提交于 2019-12-11 00:21:51

问题


I want to get current location, when app went into sleep mode or Background.Currently i am using > Plugin.Geolocator.CrossGeolocator.Current DLL in App OnSleep() , But Its Working Till Android 8.1 but Android Pie Not updating current Location Values.. How to Achieve in Android Pie?

Sample Code :

 protected override async void OnSleep()
 {
 var minute = TimeSpan.FromSeconds(30);
 Device.StartTimer(minute, () =>
 { 
 await Task.Run(async () =>
 {
 TimeSpan t = TimeSpan.FromSeconds(10);
 var locator = Plugin.Geolocator.CrossGeolocator.Current;
 locator.DesiredAccuracy = 5;
 var position = await locator.GetPositionAsync(timeout: t); 
 //API Method
 });
 });
 }

回答1:


You can not just spin up a thread and expect it to keep running after the OS places the app into the background or battery saving mode.

You will have to setup an Android Service (and foreground it) at a minimum to handle your location updates.

Background Execution Limits

https://developer.android.com/about/versions/oreo/background

Foreground Services

  • https://docs.microsoft.com/en-us/xamarin/android/app-fundamentals/services/foreground-services

Schedule tasks with WorkManager

  • https://developer.android.com/about/versions/pie/android-9.0


来源:https://stackoverflow.com/questions/54360314/xamarin-forms-android-pie-not-getting-current-location-in-app-background-sleep-m

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!