Skip to content

Commit

Permalink
5 minutes
Browse files Browse the repository at this point in the history
fixes #89
fixes #94
  • Loading branch information
jamesdiacono committed Mar 2, 2023
1 parent c85b62a commit 941a237
Showing 1 changed file with 21 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import android.os.Build;
import android.os.IBinder;
import android.provider.Settings;
import android.view.View;

import com.getcapacitor.JSObject;
import com.getcapacitor.Logger;
Expand Down Expand Up @@ -332,6 +333,26 @@ protected void handleOnResume() {
protected void handleOnPause() {
if (service != null) {
service.onActivityStopped();

// There have been reports (#89 and #94) that locations cease to be delivered
// after 5 minutes in the background, at least with Capacitor 4. The locations
// arrive on the native side, but are not delivered to the WebView until it
// returns the foreground, at which point the locations are all delivered at
// once.

// This hack comes from github.com/angeloraso/capacitor-plugin-background-mode,
// and works by tricking the WebView into thinking it's visible even when it's
// not. This prevents it going to sleep, keeping its event loop responsive
// indefinitely.

new Thread(() -> {
try {
Thread.sleep(1000);
this.bridge.getWebView().dispatchWindowVisibilityChanged(
View.VISIBLE
);
} catch (Exception ignore) {}
}).start();
}
stoppedWithoutPermissions = !hasRequiredPermissions();
super.handleOnPause();
Expand Down

2 comments on commit 941a237

@ondrej-tuhacek
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This commit is in v1.2.12 and is no longer there since v1.2.13, why is that? It works good, was there any problem with this solution?

@diachedelic
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.