3

我想在后台获取设备的地理位置,即使应用程序终止。Flutter 提供了 Isolates 和 AlarmManager,作为这样做的一种方式。

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

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

我正在使用插件:- 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 方法之前设置此标志。

如果这不起作用,我需要帮助来了解如何实现我的目标。

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

4

1 回答 1

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

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

于 2019-05-16T15:42:38.690 回答