flutter-geolocator icon indicating copy to clipboard operation
flutter-geolocator copied to clipboard

Stopping Background Location Service and Unregistering Listeners

Open mayank-vinove opened this issue 1 year ago • 9 comments

Please check the following before submitting a new issue.

Please select for which platform(s) you need help

  • [X] Android
  • [ ] iOS
  • [ ] Linux
  • [ ] macOS
  • [ ] Web
  • [ ] Windows

Your question

our Android application initiates background location updates upon user login. However, upon user logout, it's essential to gracefully stop the background service responsible for location updates and unregister all associated listeners to optimize resource usage and ensure proper application behavior.

So How can we stop background service and dispose all listeners ( stream) in case of user logout or we want to stop listning the location on simple button click

Version

12.0.0

mayank-vinove avatar Jun 25 '24 11:06 mayank-vinove

any update??

mayank-vinove avatar Jul 02 '24 04:07 mayank-vinove

Is there any update?

I also want cancel the foreground service when the user logs out and start it again when the user re-logs in. Additionally, the foreground service notification should also close when the service is canceled.

Is there any predefined method for this?

mayank-vinove avatar Jul 11 '24 04:07 mayank-vinove

@TimHoogstrate please check this.

mayank-vinove avatar Jul 11 '24 08:07 mayank-vinove

@TimHoogstrate

Summary: Currently, the Geolocator class does not provide a way to access the current active stream subscription. This creates challenges when attempting to manage background location updates, particularly when the app is killed and then reopened.

Proposed Enhancement: It would be highly beneficial to have the ability to retrieve and manage the active stream subscription directly from the Geolocator class. This would allow developers to programmatically stop or cancel the foreground service or stream when the app reopened, The primary goal is to enable better control over background location updates and ensure efficient resource management.

Use Case:

Background Location Updates: When background location updates are enabled and the app is killed, there is no straightforward way to access the active stream subscription upon reopening the app. Providing access to this active stream would allow for proper management of the service. User Logout: On user logout or any specific event, the ability to retrieve and cancel the active stream subscription would ensure that the location updates are stopped as required.

Suggested Implementation:

Introduce a method in the Geolocator class to get the current active stream subscription so it can be handle later from anywhere.

mayank-vinove avatar Aug 08 '24 11:08 mayank-vinove

Dear @mayank-vinove,

Is cancelling the position stream not sufficient like in the example app?

Kind regards,

TimHoogstrate avatar Aug 19 '24 11:08 TimHoogstrate

If the user starts location updates and then kills the app, upon reopening, they lose access to the active stream, making it impossible to cancel.

Possible Solutions: A. Implement a method to return the active stream so that the user can cancel it.

B. Create a method to cancel all active streams and clear the foreground service.

Note : Assume we are not using any third-party plugins like foreground_service or work_manager.

mayank250004 avatar Sep 02 '24 08:09 mayank250004

I am facing the same issue, although in my case I'm currently testing on iOS. If I start a stream that tracks the user in the background, and the user then puts the app in the background, when they reopen the app I find I lost my refrence to the active stream subscription and I have no way to close it. Is there any workaround to this?

anthealb avatar Oct 07 '24 12:10 anthealb