Основываясь на обзоре последних рекомендаций Google, кажется, что Google должен разрешить нашему приложению отслеживать устройство, когда оно находится в фоновом режиме, но это не так. Фактически, включение служб определения местоположения на переднем и заднем плане, казалось, работало месяц или два назад. Мы попробовали пакеты местоположения и геолокации, но безрезультатно. Любой из них немедленно прекращает отслеживание, когда приложение находится в фоновом режиме, и мы получаем резкий скачок местоположения, когда приложение возвращается на передний план.
Вот что мы попробовали
AndroidManifest.xml
Код: Выделить всё
...
...
Код: Выделить всё
...
serviceEnabled = await Geolocator.isLocationServiceEnabled();
if (!serviceEnabled) {
return Future.error('Location services are disabled.');
}
permission = await Geolocator.checkPermission();
if (permission == LocationPermission.denied) {
permission = await Geolocator.requestPermission();
if (permission == LocationPermission.denied) {
return Future.error('Location permissions are denied');
}
}
//Permission returns denied Location
//this is due to the background permission in the android manifest
//turn off background permission and the permission is allowed
if (permission == LocationPermission.deniedForever)
return Future.error(
'Location permissions are permanently denied, we cannot request permissions.');
}
...
Код: Выделить всё
_serviceEnabled = await location.serviceEnabled();
if (!_serviceEnabled) {
_serviceEnabled = await location.requestService();
if (!_serviceEnabled) {
print('Service could not be enabled');
return false;
}
// This code throws and exception even if I respond to the
// Google prompts and I select allow location service and all the time access
try {
await location.enableBackgroundMode(enable: true);
} catch(error) {
print("Can't set background mode");
}
Подробнее здесь: https://stackoverflow.com/questions/685 ... th-flutter