在Flutter应用中将辅助定位中的地理位置作为后台任务

时间:2019-04-20 20:52:10

标签: dart flutter geolocation dart-isolates

我想在后台获取设备的地理位置,即使应用程序终止也是如此。 Flutter提供了隔离以及AlarmManager来实现。

我的最终目标还是要获得设备的地理位置,即使应用程序已终止。

我经历了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

这是在辅助隔离中崩溃的行。

geolocation插件具有其他配置,在其中我可以强制LocationManager跳入。这不会崩溃,但是会在应用终止时立即挂断。以下是在geolocator插件中强制它的方式:- https://github.com/BaseflowIT/flutter-geolocator/blob/b869f3221c890c2ae87b22d0384577fd902d38ca/lib/geolocator.dart#L64

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

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

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

1 个答案:

答案 0 :(得分:0)

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)。

仅需在此处请求API即可进行活动:https://github.com/BaseflowIT/flutter-google-api-availability/blob/96ffe46a7cf8d81bfa107ca83ce2aaf8a5ff4847/android/src/main/java/com/baseflow/googleapiavailability/GoogleApiAvailabilityPlugin.java#L67

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