dart - 在 Flutter 应用程序中作为后台任务在二级隔离中获取地理位置

标签 dart flutter geolocation dart-isolates

我想在后台获取设备的 GeoLocation,即使应用已终止。 Flutter 提供 Isolates 和 AlarmManager 来实现这一点。

同样,我的最终目标是即使在应用程序已终止时也能获取设备的 GeoLocation。

我已经经历了https://medium.com/flutter-io/executing-dart-in-the-background-with-flutter-plugins-and-geofencing-2b3e40a1a124 但并不完全理解,因为我是 Flutter 的新手。如果这是唯一的方法,请帮助我理解它。

我正在使用插件:- https://pub.dartlang.org/packages/geolocator https://pub.dartlang.org/packages/flutter_isolate

问题来了,正如我所见,当 Geolocator 在内部检查 GoogleApiAvailability 插件后尝试获取位置时,使用辅助隔离内的“Channel.invokeMethod”。它抛出的错误是:-

[  +81 ms] E/GooglePlayServicesUtil(11608): The Google Play services resources were not found. Check your project configuration to ensure that the resources are included.
[        ] E/MethodChannel#flutter.baseflow.com/google_api_availability/methods(11608): Failed to handle method call
[        ] E/MethodChannel#flutter.baseflow.com/google_api_availability/methods(11608): java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
[        ] E/MethodChannel#flutter.baseflow.com/google_api_availability/methods(11608):     at com.google.android.gms.common.GooglePlayServicesUtilLight.isGooglePlayServicesAvailable(Unknown Source:12)
[        ] E/MethodChannel#flutter.baseflow.com/google_api_availability/methods(11608):     at com.google.android.gms.common.GoogleApiAvailabilityLight.isGooglePlayServicesAvailable(Unknown Source:5)
[        ] E/MethodChannel#flutter.baseflow.com/google_api_availability/methods(11608):     at com.google.android.gms.common.GoogleApiAvailability.isGooglePlayServicesAvailable(Unknown Source:94)

看看:- https://github.com/BaseflowIT/flutter-geolocator/blob/b869f3221c890c2ae87b22d0384577fd902d38ca/lib/geolocator.dart#L75

在以下位置调用 GoogleApiAvailability:- https://github.com/BaseflowIT/flutter-google-api-availability/blob/96ffe46a7cf8d81bfa107ca83ce2aaf8a5ff4847/lib/src/google_api_availability.dart#L23

这是在二级隔离上崩溃的行。

地理定位插件有一个额外的配置,我可以在其中强制 LocationManager 跳入。它不会崩溃,但会在应用程序终止后立即挂断。以下是在地理定位器插件中强制执行的方式:- https://github.com/BaseflowIT/flutter-geolocator/blob/b869f3221c890c2ae87b22d0384577fd902d38ca/lib/geolocator.dart#L64

可以在调用 getLocation 方法之前设置此标志。

如果这行不通,我需要有关如何实现我的目标的帮助。

理想情况下,它应该给我二级隔离区的位置。我需要了解相同所需的正确配置。

最佳答案

GoogleApiAvailability 插件使用当前事件来检查 API 是否可用,请参见此处:https://github.com/BaseflowIT/flutter-google-api-availability/blob/96ffe46a7cf8d81bfa107ca83ce2aaf8a5ff4847/android/src/main/java/com/baseflow/googleapiavailability/GoogleApiAvailabilityPlugin.java#L50

我想这是错误的,因为 isGooglePlayServicesAvailable() 方法需要上下文,而不是事件(参见:https://developers.google.com/android/reference/com/google/android/gms/common/GoogleApiAvailability)。

Activity 只需要在此处请求 API:https://github.com/BaseflowIT/flutter-google-api-availability/blob/96ffe46a7cf8d81bfa107ca83ce2aaf8a5ff4847/android/src/main/java/com/baseflow/googleapiavailability/GoogleApiAvailabilityPlugin.java#L67

这不适用于隔离(或服务),因此如果事件不可用,插件应改用应用程序上下文。

关于dart - 在 Flutter 应用程序中作为后台任务在二级隔离中获取地理位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55777639/

相关文章:

firebase - 如何在 flutter 和 firebase 中注册新用户?

java - Android 上的 Flutter path_provider 错误,而在 iOS 上运行完美

flutter - 如何使用 build_runner 从生成的文件中清除项目

javascript - 使用应用程序中嵌入的浏览器时,无法从 Android 上的网页访问地理位置

dart - 如何在 DART 语言中实现 DOM 数据绑定(bind)

flutter - 单击时如何将ExpansionTile小部件与其他小部件重叠?

android - 在flutter中从image_picker包中打开相机会导致真实设备上的应用程序崩溃,但在模拟器(android)中运行良好

flutter - 在Web的Flutter中实现访客计数器

php - html5地理位置和地址

javascript - 为什么我的 iPhone Web 应用程序中两次被要求使用 GPS