rekabhq / background_locator

A Flutter plugin for updating location in background.
MIT License
289 stars 330 forks source link

callback triggerd twice when closing app and reopen again #273

Open daviddagan opened 3 years ago

daviddagan commented 3 years ago

I'm killing the service when the app is killed as you explained to me in your guide via the AndroidManifest. and using the service just as you wrote it in the example. the service work great til im reopen the app again and then the service throws location twice

mehdok commented 3 years ago

Hi @daviddagan Thank you for opening an issue;

Please provide more information, eg. your platform (Android, iOS). Also, It would be nice if you could provide a minimal working example, so I can see what is happening exactly;

daviddagan commented 3 years ago

hello @mehdok I'm using an android emulator for this app and I register the isolate just like your example:

  DataCollectorBloc(this.permissionsCubit, this.dataRecordRepository) : super(LocationInit()) {
    if (IsolateNameServer.lookupPortByName(LocationServiceRepository.isolateName) != null) IsolateNameServer.removePortNameMapping(LocationServiceRepository.isolateName);

    IsolateNameServer.registerPortWithName(port.sendPort, LocationServiceRepository.isolateName);

    port.listen((dynamic data) => _updateDataRecordList(data));

    initPlatformState();

    permissionsStream = permissionsCubit.stream.listen((permissionsState) {
      if (permissionsState is PermissionsGranted && permissionsState.statuses.keys.contains(Permission.phone) && permissionsState.statuses.keys.contains(Permission.location)) {
        _onStart();
      }
    });
  }

the app working fine till I kill the app. but when I click and reopen the app I'm getting twice the location data from the callback

mehdok commented 3 years ago

is the data different each time? or it is giving the same data twice? It would be a great help if you could test on a real device and mention the Android version;

daviddagan commented 3 years ago

same data twice, it happens only in emulator. I think the problem is when i'm still in debug mode and shut down and reopen the app in the emulator. only then i get ping twice

‫בתאריך יום ה׳, 16 בספט׳ 2021 ב-10:50 מאת ‪mehdi sohrabi‬‏ <‪ @.***‬‏>:‬

is the data different each time? or it is giving the same data twice? It would be a great help if you could test on a real device and mention the Android version;

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/rekab-app/background_locator/issues/273#issuecomment-920671863, or unsubscribe https://github.com/notifications/unsubscribe-auth/AH6ZIWUMGP2PE66CQFBWJLDUCGOVPANCNFSM5D5MWT3A . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

andrewzakhartchouk commented 2 years ago

@mehdok I can confirm this occurs on real devices, regardless of Android version. But I did mainly test it on Android version 7 and 11. It happens as a result of inserting android:stopWithTask="true" into the AndroidManifest.xml. When you terminate the application, nothing appears in the logs. Interestingly Android Studio doesn't lose connection with the device on first disconnection, suggesting it is still running in the background. Upon restarting the application, The GPS pin icon turns on at the top (suggesting Location Services is running) and Background Locator logs start rolling again, eg:

D/plugin  ( 6567): sendLocationEvent {callback=8636740407, location={altitude=0.0, heading=90.0, latitude=35.6841183, accuracy=5.0, speed_accuracy=0.5, time=1.635233960267E12, is_mocked=false, speed=0.0, longitude=139.7775333}}
I/flutter ( 6567): 7 location in dart: LocationDto{latitude: 35.6841183, longitude: 139.7775333, accuracy: 5.0, altitude: 0.0, speed: 0.0, speedAccuracy: 0.5, heading: 90.0, time: 1635233960267.0, isMocked: false}

However, checking both BackgroundLocator.isServiceRunning()); and BackgroundLocator.isRegisterLocationUpdate()); returns false. When you start the BackgroundLocator and start listening to the ReceivePort(), it gets 2 identical calls.

As was mentioned in #139, it is believed that the inherent callback is being triggered again. I have found that after closing and re-opening the app a second time in a row seems to stop the callback completely (as due the the GPS pin no longer showing at the top of the phone), although I can't confirm as Android Studio loses connection to the device.

I think this can be solved by fixing the isServiceRunning to properly detect when it is running, even after being restarted.

daviddagan commented 2 years ago

Thanks for the feedback! let me know ig fixed this issue

‫בתאריך יום ג׳, 26 באוק׳ 2021 ב-11:26 מאת ‪Andrew Zakhartchouk‬‏ <‪ @.***‬‏>:‬

@mehdok https://github.com/mehdok I can confirm this occurs on real devices, regardless of Android version. But I did mainly test it on Android version 7 and 11. It happens as a result of inserting android:stopWithTask="true" into the AndroidManifest.xml. When you terminate the application, nothing appears in the logs. Interestingly Android Studio doesn't lose connection with the device on first disconnection, suggesting it is still running in the background. Upon restarting the application, The GPS pin icon turns on at the top (suggesting Location Services is running) and Background Locator logs start rolling again, eg:

D/plugin ( 6567): sendLocationEvent {callback=8636740407, location={altitude=0.0, heading=90.0, latitude=35.6841183, accuracy=5.0, speed_accuracy=0.5, time=1.635233960267E12, is_mocked=false, speed=0.0, longitude=139.7775333}} I/flutter ( 6567): 7 location in dart: LocationDto{latitude: 35.6841183, longitude: 139.7775333, accuracy: 5.0, altitude: 0.0, speed: 0.0, speedAccuracy: 0.5, heading: 90.0, time: 1635233960267.0, isMocked: false}

However, checking both BackgroundLocator.isServiceRunning()); and BackgroundLocator.isRegisterLocationUpdate()); returns false. When you start the BackgroundLocator and start listening to the ReceivePort(), it gets 2 identical calls.

As was mentioned in #139 https://github.com/rekab-app/background_locator/issues/139, it is believed that the inherent callback is being triggered again. I have found that after closing and re-opening the app a second time in a row seems to stop the callback completely (as due the the GPS pin no longer showing at the top of the phone), although I can't confirm as Android Studio loses connection to the device.

I think this can be solved by fixing the isServiceRunning to properly detect when it is running, even after being restarted.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/rekab-app/background_locator/issues/273#issuecomment-951704420, or unsubscribe https://github.com/notifications/unsubscribe-auth/AH6ZIWX756I2TPVCTM6MHX3UIZQ3BANCNFSM5D5MWT3A . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

3wer3wer commented 2 years ago

I think this can be solved by fixing the isServiceRunning to properly detect when it is running, even after being restarted.

I faced the same issue as @andrewzakhartchouk . It would be very helpful to detect if there is already a running service. Otherwise I my function would be triggered multiple times.

Thank you very much from Germany!