我想要在后台获取设备的GeoLocation,即使应用程序已终止。作为一种方法,Flutter提供了隔离,以及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
问题来了,正如我所看到的,当地理定位器在内部检查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)
在以下位置调用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方法之前设置此标志。
如果这不起作用,我需要帮助来实现我的目标。
理想情况下,它应该给我一个二级隔离的位置。我需要了解相同配置所需的正确配置。
发布于 2019-05-16 15:42:39
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不可用,插件将使用Application context。
https://stackoverflow.com/questions/55777639
复制