flutter_background_service icon indicating copy to clipboard operation
flutter_background_service copied to clipboard

Can Geolocator run with this plugin at the background?

Open Ryan45758 opened this issue 3 years ago • 9 comments

Can Geolocator run with this plugin at the background?

Ryan45758 avatar May 31 '21 14:05 Ryan45758

Did you try to use it with geolocator? I look at the geolocator plugin code, and for me, it should work. That plugin is ActivityAware, so you should just request required location permissions when app is in foreground, and then the plugin itself should work even if app is in background, when the plugin is not attached to any activity.

I am not sure, but maybe you will have to wait until the permission are granted before you start using geolocator in service, or try to init foreground service only when all the permissions are granted.

EDIT. I could be mistaken. Android 10+ requires service to have type location in android manifest, if it wants to access location. This plugin does not have service type filled, so you probably won't be able to access location in background mode on android 10+.

incloudss avatar Jun 04 '21 09:06 incloudss

I tested this today, and i can confirm it works like i said - you can start foreground service, get locations updates when app is in foreground, but the moment you minimize it - location updates stop, because service is not marked with location type in android manifest in plugin package, and therefore is not able to access location.

@ekasetiawans Maybe we should extend the android manifest to contain all service types like: image But by default service would start without any type? image

I am not sure though if there is no problem with marking service as foreground serivce in android manifest and starting it as background service.

incloudss avatar Jun 05 '21 09:06 incloudss

@incloudss looks like it's starting to be needed on android 10 and up, unfortunately i only have a device with android 9 and no problem regarding location on foreground service.

ekasetiawans avatar Jun 07 '21 14:06 ekasetiawans

I am using geolocator package and it work fine in the background

officialismailshah avatar Oct 21 '21 19:10 officialismailshah

I'm using it, works perfectly.

BasharShehab avatar Oct 28 '21 15:10 BasharShehab

I am using geolocator package and it work fine in the background I'm using it, works perfectly.

Hello, could you share an example of how to implement, thanks

byblady avatar Oct 29 '21 21:10 byblady

@byblady

Depending on your usage, I'd recommend using BackgroundLocator instead. However, if your use case needs Geolocator for some reason, use it in combination with Flutter Background Service.

Follow the documentation, it is easy to implement. If you still need help afterwards, ping me again.

Note: after implementation, you might get a few non-fatal execptions when you quit to background. Ignore them, I don't know why the exceptions show, but the library and the functions I needed still worked.

BasharShehab avatar Oct 30 '21 01:10 BasharShehab

Hello. I'm trying to implement both flutter_background_service and geolocator but not able to. I have followed closely the example code and include this part to fetch location periodically in the onStart function but I keep getting the Missing Pluggin Exception, method getCurrentLocation is not implemented on channel. The same codes work well if I implement it to fetch location on button press. I have tried flutter clean and flutter run and that didn't help either. Any idea how I can work with both plugins?

Timer.periodic(const Duration(seconds: 30), (timer) async {
      var position = await Geolocator.getCurrentPosition(
        forceAndroidLocationManager: true,
        desiredAccuracy: LocationAccuracy.high,
      );
      service.invoke('updateLocation', position as Map<String, dynamic>);
    });

thanh-cao avatar Apr 20 '22 21:04 thanh-cao

Hello. I'm trying to implement both flutter_background_service and geolocator but not able to. I have followed closely the example code and include this part to fetch location periodically in the onStart function but I keep getting the Missing Pluggin Exception, method getCurrentLocation is not implemented on channel. The same codes work well if I implement it to fetch location on button press. I have tried flutter clean and flutter run and that didn't help either. Any idea how I can work with both plugins?

Timer.periodic(const Duration(seconds: 30), (timer) async {
      var position = await Geolocator.getCurrentPosition(
        forceAndroidLocationManager: true,
        desiredAccuracy: LocationAccuracy.high,
      );
      service.invoke('updateLocation', position as Map<String, dynamic>);
    });

same with me missing plugine exception

Ryan45758 avatar Apr 21 '22 00:04 Ryan45758