capacitor-community / background-geolocation

A Capacitor plugin that sends you geolocation updates, even while the app is in the background.
MIT License
180 stars 56 forks source link

Problem with application closing #59

Closed bokolob closed 1 year ago

bokolob commented 2 years ago

Describe the bug Hello. When I close my application the background service keeps running. But it seems that it doesn't do anything. After that, when I starts application again It couldn't handle GPS events and produces the next stacktrace.

Moreover, application becomes unusable until device reboot.

2022-03-27 14:23:17.071 21353-21366/com.gps W/MessageQueue: Handler (com.google.android.gms.common.api.internal.ListenerHolder$zaa) {2e37b66} sending message to a Handler on a dead thread
    java.lang.IllegalStateException: Handler (com.google.android.gms.common.api.internal.ListenerHolder$zaa) {2e37b66} sending message to a Handler on a dead thread
        at android.os.MessageQueue.enqueueMessage(MessageQueue.java:600)
        at android.os.Handler.enqueueMessage(Handler.java:631)
        at android.os.Handler.sendMessageAtTime(Handler.java:600)
        at android.os.Handler.sendMessageDelayed(Handler.java:570)
        at android.os.Handler.sendMessage(Handler.java:507)
        at com.google.android.gms.common.api.internal.ListenerHolder.notifyListener(Unknown Source)
        at com.google.android.gms.internal.location.zzat.onLocationAvailability(Unknown Source)
        at com.google.android.gms.location.zzv.dispatchTransaction(Unknown Source)
        at com.google.android.gms.internal.location.zzb.onTransact(Unknown Source)
        at android.os.Binder.execTransact(Binder.java:453)
2022-03-27 14:23:17.071 21353-21366/com.gps W/MessageQueue: Handler (com.google.android.gms.common.api.internal.ListenerHolder$zaa) {7f34263} sending message to a Handler on a dead thread
    java.lang.IllegalStateException: Handler (com.google.android.gms.common.api.internal.ListenerHolder$zaa) {7f34263} sending message to a Handler on a dead thread
        at android.os.MessageQueue.enqueueMessage(MessageQueue.java:600)
        at android.os.Handler.enqueueMessage(Handler.java:631)
        at android.os.Handler.sendMessageAtTime(Handler.java:600)
        at android.os.Handler.sendMessageDelayed(Handler.java:570)
        at android.os.Handler.sendMessage(Handler.java:507)
        at com.google.android.gms.common.api.internal.ListenerHolder.notifyListener(Unknown Source)
        at com.google.android.gms.internal.location.zzat.onLocationAvailability(Unknown Source)
        at com.google.android.gms.location.zzv.dispatchTransaction(Unknown Source)
        at com.google.android.gms.internal.location.zzb.onTransact(Unknown Source)
        at android.os.Binder.execTransact(Binder.java:453)

Smartphone (please complete the following information):

diachedelic commented 2 years ago

When you say "close my application" do you mean kill the application, or just send it to the background? What if you go to Settings and press "Force stop"?

bokolob commented 2 years ago

I meant closing :) I swiped it out from apps list :) Sorry, I’m using android just for test my application for the first time.

And I checked with debugger - Service.selfStop was actually called. Maybe watchers should be removed before exit.

diachedelic commented 2 years ago

So you killed the app via the "task manager"?

bokolob commented 2 years ago

No, not killed, I tried to explain in my previous comment

bokolob commented 2 years ago

I didn’t use any special software like task managers

diachedelic commented 2 years ago

I meant closing :) I swiped it out from apps list

Sorry I missed that bit (that is indeed what I meant by "task manager"). If you remove the watchers before closing the app does it help?

bokolob commented 2 years ago

I haven’t tried yet. I’ve got this idea just now :) And by the way - if I build plugin by myself, how do I add to capacitor project? Should I somehow change gradle configuration?

diachedelic commented 2 years ago

For experimenting, just modify BackgroundGeolocation.java and BackgroundGeolocationService.java from your editor. These changes will be wiped by reinstalling the background-geolocation NPM package.

kantharia commented 2 years ago

Is there any app close event in Ionic? Is there any way to removeWatcher ?

MasDevProject commented 1 year ago

Same issue here killing the app swiping it up from the recent apps. I didn't find a way to call removeWatcher if the app is being killed by the user. Did anyone find a solution?

diachedelic commented 1 year ago

What device and Android version are you using?

MasDevProject commented 1 year ago

@diachedelic Pixel 5 with API 33 (Android 13)

diachedelic commented 1 year ago

I am not able to reproduce this bug. If you can reliably reproduce this, please add the following to BackgroundGeolocationService.java and see if onUnbind gets called when the application is killed. If it does, we can make the service self-destruct.

    @Override
    public boolean onUnbind(Intent intent) {
        Logger.error("onUnbind called");
        return true;
    }
MasDevProject commented 1 year ago

@diachedelic it is called! With this code the error is fixed:

    @Override
    public boolean onUnbind(Intent intent) {
      Logger.error("onUnbind called!!!!!!!!");

    for (Watcher watcher : watchers) {
        watcher.client.removeLocationUpdates(watcher.locationCallback);
        watchers.remove(watcher);
        if (getNotification() == null) {
          stopForeground(true);
        }
      }

      return true;
    }

Will you update the main repo with this fix?

diachedelic commented 1 year ago

Can you test this code please?

    @Override
    public boolean onUnbind(Intent intent) {
        for (Watcher watcher : watchers) {
            watcher.client.removeLocationUpdates(watcher.locationCallback);
            watchers.remove(watcher);
        }
        stopSelf();
        return false;
    }
MasDevProject commented 1 year ago

@diachedelic It works!

diachedelic commented 1 year ago

This has been published in v1.2.6.