【问题标题】:How to track location in background with Flutter?如何使用 Flutter 在后台跟踪位置?
【发布时间】:2021-10-05 15:31:12
【问题描述】:

我一直在一个使用 Flutter 开发应用程序的团队中工作。我们需要使用 GPS 跟踪行进的距离,但挑战在于 Flutter 不再允许前景和背景位置跟踪(即使是粗略的准确度)。

根据对最新 Google 指南的审核,Google 似乎应该允许我们的应用在设备处于后台时对其进行跟踪,但事实并非如此。事实上,打开前台和后台定位服务似乎在一两个月前就开始起作用了。我们尝试了locationgeolocation 软件包,但均无济于事。当应用程序在后台时,它们中的任何一个都会立即停止跟踪,而当应用程序返回到前台时,我们的位置会发生很大的跳跃。

这是我们尝试过的方法

AndroidManifest.xml
...
<uses-permission android:name="android.permission.ACCESS_COURSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION"/>
...

使用地理位置的应用代码
...
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");
}

任何关于如何实现这一目标的指导将不胜感激。

【问题讨论】:

    标签: android ios flutter geolocation location


    【解决方案1】:

    Flutter 本身还不支持后台本地化。

    我见过一些特定于 android 的表单,但没有任何效果。

    但积极寻找,我发现了这个库:

    https://pub.dev/packages/flutter_background_geolocation

    这是一个付费库,用于生产环境(适用于 android 密钥。iOS 是免费的)。而且效果很好。

    尽管有成本(这是独一无二的,而且是终生的),但成本效益非常好。

    强烈推荐。

    注意:他们的文档和支持非常棒。

    【讨论】:

      【解决方案2】:

      目前,我正在使用 background_fetch,因为每个人都买不起flutter_background_geolocation定价顺便说一句这个插件也来自同一家公司

      background_fetch: ^0.7.2 
      

      到目前为止,它在 Android 中运行良好,但在 ios 中运行良好,但作为 Apple 的后台进程算法需要一些时间,一旦它被使用,那么它在 Apple 中也会很好。

      这是链接-: https://pub.dev/packages/background_fetch

      设置

         int status = await BackgroundFetch.configure(
          BackgroundFetchConfig(
              minimumFetchInterval: 30,
              stopOnTerminate: true,
              enableHeadless: false,
              startOnBoot: false,
              requiresBatteryNotLow: false,
              requiresCharging: false,
              requiresStorageNotLow: false,
              requiresDeviceIdle: false,
              requiredNetworkType: NetworkType.NONE),
              (taskId){
               // This is the fetch-event callback.
              },
          
             (taskId){
            // This task has exceeded its allowed running time.
            // You must stop what you're doing and immediately finish(taskId)
          });
          print('[BackgroundFetch] configure success: $status');
      

      【讨论】:

        猜你喜欢
        • 2013-07-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-12-21
        • 2013-05-16
        • 1970-01-01
        • 1970-01-01
        • 2017-04-13
        相关资源
        最近更新 更多