Xamarin在应用程序后台睡眠模式下无法获得Android Pie的当前位置 [英] Xamarin Forms Android Pie Not Getting Current Location in App Background Sleep Mode

查看:97
本文介绍了Xamarin在应用程序后台睡眠模式下无法获得Android Pie的当前位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当应用进入睡眠模式或背景时,我想获取当前位置.当前我在应用OnSleep()中使用> Plugin.Geolocator.CrossGeolocator.Current DLL,但它的工作状态直到Android 8.1,但Android Pie仍未更新当前位置值.如何在Android Pie中实现?

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?

示例代码:

 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
 });
 });
 }

推荐答案

在操作系统将应用程序置于后台或省电模式后,您不能只是旋转线程并期望它继续运行.

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.

您必须至少安装一个Android Service(并对其进行前台安装) ,以处理您的位置更新.

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

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

这篇关于Xamarin在应用程序后台睡眠模式下无法获得Android Pie的当前位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆