From 941a2370befb976583be1140f2a5b6393018dd5f Mon Sep 17 00:00:00 2001 From: James Diacono Date: Thu, 2 Mar 2023 11:10:13 +1100 Subject: [PATCH] 5 minutes fixes #89 fixes #94 --- .../BackgroundGeolocation.java | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/android/src/main/java/com/equimaps/capacitor_background_geolocation/BackgroundGeolocation.java b/android/src/main/java/com/equimaps/capacitor_background_geolocation/BackgroundGeolocation.java index 6cf75cd..0907e0b 100644 --- a/android/src/main/java/com/equimaps/capacitor_background_geolocation/BackgroundGeolocation.java +++ b/android/src/main/java/com/equimaps/capacitor_background_geolocation/BackgroundGeolocation.java @@ -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; @@ -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();