diff --git a/location/java/android/location/LocationManager.java b/location/java/android/location/LocationManager.java index a112bdd0ce03c..7d15bbd466976 100644 --- a/location/java/android/location/LocationManager.java +++ b/location/java/android/location/LocationManager.java @@ -2583,9 +2583,15 @@ public class LocationManager { } public void cancel() { + remove(); + } + + private Consumer remove() { + Consumer consumer; ICancellationSignal cancellationSignal; synchronized (this) { mExecutor = null; + consumer = mConsumer; mConsumer = null; if (mAlarmManager != null) { @@ -2605,6 +2611,8 @@ public class LocationManager { // ignore } } + + return consumer; } public void fail() { @@ -2663,16 +2671,10 @@ public class LocationManager { } private void acceptResult(Location location) { - Consumer consumer; - synchronized (this) { - if (mConsumer == null) { - return; - } - consumer = mConsumer; - cancel(); + Consumer consumer = remove(); + if (consumer != null) { + consumer.accept(location); } - - consumer.accept(location); } }