Android battery managers
Long Ping runs as a foreground service with a notification, restarts itself after kills and reboots, and checks every 15 minutes that it is still running. On stock Android that is enough. Many manufacturers add their own battery managers on top (Samsung’s “sleeping apps”, Xiaomi, Huawei, Oppo, Vivo, OnePlus and others), and those can freeze or kill any app, foreground service or not.
No library can switch these off for the user. What it can do is open the right settings screen, so the user can exempt your app in a couple of taps. dontkillmyapp.com documents what each brand does and which switches matter.
The helpers
Section titled “The helpers”All live under LongPing.deviceSettings. On iOS they are harmless no-ops or open the app’s Settings page.
| Method | Android | iOS |
|---|---|---|
isIgnoringBatteryOptimizations() |
true if the app is exempt from battery optimization (PowerManager). |
Always true. |
requestIgnoreBatteryOptimizations() |
With REQUEST_IGNORE_BATTERY_OPTIMIZATIONS declared: the direct “Allow” prompt. Without it: the system’s battery-optimization list. Resolves true if a screen opened. |
false |
openPowerManagerSettings() |
The first OEM power or auto-start screen that exists on this phone (Samsung, Xiaomi, Huawei, Oppo/Realme, Vivo, OnePlus, Asus, Letv, Meizu), else the app’s details page. Resolves true if a screen opened. |
false |
openAppSettings() |
The app’s details page. | The app’s Settings page. |
openLocationSettings() |
The system location settings. | The app’s Settings page. |
getProviderState() also reports ignoringBatteryOptimizations and powerSaveMode.
A setup screen that works
Section titled “A setup screen that works”-
After permissions are sorted, check the exemption:
const exempt = await LongPing.deviceSettings.isIgnoringBatteryOptimizations(); -
If it’s
false, explain first. One sentence on why (“so your location keeps reaching dispatch when the screen is off”), then a button:await LongPing.deviceSettings.requestIgnoreBatteryOptimizations(); -
Then the manufacturer screen, with brand-specific instructions from dontkillmyapp.com next to the button:
import { Platform } from 'react-native';if (Platform.OS === 'android') {await LongPing.deviceSettings.openPowerManagerSettings();} -
Check again when the app comes back to the foreground, and show the result.
How you’ll know it happened
Section titled “How you’ll know it happened”A frozen app simply stops producing fixes. Long Ping can’t log while it is frozen, but it logs what it sees when it runs again:
- the watchdog noting how long it has been since the last fix, and restarting location updates;
- “service restarted by the system after its process was killed”;
- the 15-minute WorkManager check starting the service again.
Read them with logger.getLog(); see Troubleshooting. On your server, the symptom
is a gap in a device’s timestamps with no stop() behind it.
Other Android notes
Section titled “Other Android notes”- Android 13+: request
POST_NOTIFICATIONS, or the tracking notification is hidden. See Installation. - Android 12+ may refuse to start a foreground service from the background
(
ForegroundServiceStartNotAllowedException). Long Ping catches it, logs it and tries again at the next 15-minute check. It never crashes your app. - Doze: the foreground service is designed to keep recording fixes while the phone dozes; uploads may wait for the system’s maintenance windows and arrive late but complete. This is one of the scenarios in the end-to-end test plan that is being run now.