Android: Stop location updates while paused

Nothing turned the GPS off when the app went to the background, so a game
that had opened it kept it running until the process died. Pause and
resume it like the camera. A start request that arrives while paused (as
the one from the permission dialog does) is deferred to resume.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
Henrik RydgårdandClaude Fable 5.1 committed 2026-10-08 20:11:23 +02:00
1 parent 90c7cd2a03
commit cec72d46c5
2 files changed
+37 -1

No files matched your search

@@ -22,6 +22,9 @@ class LocationHelper implements LocationListener {
private static final int GPGGA_ALTITUDE_INDEX = 9;
private final LocationManager mLocationManager;
private boolean mLocationEnable;
// What the game has asked for, which is what we go back to on resume.
private boolean mLocationWanted = false;
private boolean mPaused = false;
private GpsStatus.Listener mGpsStatusListener;
private GnssStatus.Callback mGnssStatusCallback;
private OnNmeaMessageListener mNmeaMessageListener;
@@ -35,6 +38,33 @@ class LocationHelper implements LocationListener {
}
void startLocationUpdates() {
mLocationWanted = true;
if (mPaused) {
Log.d(TAG, "startLocationUpdates: paused, deferring to resume");
return;
}
startListening();
}
void stopLocationUpdates() {
mLocationWanted = false;
stopListening();
}
// Don't keep the GPS going while we're in the background. The game isn't running to see it anyway.
void pause() {
mPaused = true;
stopListening();
}
void resume() {
mPaused = false;
if (mLocationWanted) {
startListening();
}
}
private void startListening() {
Log.d(TAG, "startLocationUpdates");
if (!mLocationEnable) {
boolean isGPSEnabled = false;
@@ -71,7 +101,7 @@ class LocationHelper implements LocationListener {
}
}
void stopLocationUpdates() {
private void stopListening() {
Log.d(TAG, "stopLocationUpdates");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
if (mGnssStatusCallback != null) {
@@ -1010,6 +1010,9 @@ public class PpssppActivity extends AppCompatActivity implements SensorEventList
Log.i(TAG, "Calling mCameraHelper.pause");
mCameraHelper.pause();
}
if (mLocationHelper != null) {
mLocationHelper.pause();
}
Log.i(TAG, "onPause end");
}
@@ -1028,6 +1031,9 @@ public class PpssppActivity extends AppCompatActivity implements SensorEventList
if (mCameraHelper != null) {
mCameraHelper.resume();
}
if (mLocationHelper != null) {
mLocationHelper.resume();
}
updateAudioFocus(this.audioManager, this.audioFocusChangeListener);
NativeApp.resume();