capacitor-community/background-geolocation

Crash onUnbind Android

lugaru90 opened this issue · 2 comments

Describe the bug
When the App comes back to foreground, the Background Gelolocation is stopped. On Some Android devices this produces a Crash.

"com.equimaps.capacitor_background_geolocation.BackgroundGeolocationService.onUnbind".

"java.util.ConcurrentModificationException"


Exception java.lang.RuntimeException:
  at android.app.ActivityThread.handleUnbindService (ActivityThread.java:4720)
  at android.app.ActivityThread.access$1900 (ActivityThread.java:301)
  at android.app.ActivityThread$H.handleMessage (ActivityThread.java:2195)
  at android.os.Handler.dispatchMessage (Handler.java:106)
  at android.os.Looper.loop (Looper.java:246)
  at android.app.ActivityThread.main (ActivityThread.java:8633)
  at java.lang.reflect.Method.invoke
  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:602)
  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1130)
Caused by java.util.ConcurrentModificationException:
  at java.util.HashMap$HashIterator.nextNode (HashMap.java:1441)
  at java.util.HashMap$KeyIterator.next (HashMap.java:1465)
  at com.equimaps.capacitor_background_geolocation.BackgroundGeolocationService.onUnbind (BackgroundGeolocationService.java:51)
  at android.app.ActivityThread.handleUnbindService (ActivityThread.java:4704)

Smartphone (please complete the following information):

  • Device: Galaxy A40, A54, S22, S21
  • OS: Android 11, 13

Additional context

  this.stateListener = App.addListener('appStateChange', ({ isActive }) => {
      if (isActive) {
        // In Foreground use @capacitor/geolocation
        this.startGPSTimer();
        if (Capacitor.isNativePlatform()) {
          this.stopBackgroundGPS();
        }
      } else {
        // In Background use @capacitor-community/background-geolocation
        this.stopGPSTimer();
        if (Capacitor.isNativePlatform()) {
          this.initBackgroundGPS();
      }
    });


  stopBackgroundGPS() {
    if (this.watcherId) {
      this.backgroundGeolocation.removeWatcher({
        id: this.watcherId,
      });
    }
  }

  initBackgroundGPS() {
    if (Capacitor.isNativePlatform()) {
      const aThis = this;
      if (!this.backgroundGeolocation) {
        this.backgroundGeolocation =
          registerPlugin<BackgroundGeolocationPlugin>('BackgroundGeolocation');
      }
      this.backgroundGeolocation
        .addWatcher(
          {
            backgroundMessage: 'SECRET HERE',
            backgroundTitle: 'SECRET HERE',
            requestPermissions: true,
            stale: false,
            distanceFilter: 100,
          },
          function callback(location, error) {
            if (error) {
               ...
            }
            aThis.coordinates = location;
            aThis.updateProjects(false, true);
          }
        )
        .then(function after_the_watcher_has_been_added(watcher_id) {
          this.watcherId = watcher_id;
        });
    }
  }

Any ideas?

I think this crash occurred only at the moment the app was killed, so it probably wasn't causing too much havoc. Regardless, it is now fixed in v1.2.11, thanks for reporting.

I can approve that version 1.2.11 fixed the issue. Thanks!