@@ -148,6 +148,13 @@ private void startGpsLocListener() {
148148 mLocManager .requestLocationUpdates (LocationManager .GPS_PROVIDER , LOCATION_INTERVAL_MS , 0 , this );
149149 }
150150
151+ private void stopGpsLocListener () {
152+ if (ActivityCompat .checkSelfPermission (this , Manifest .permission .ACCESS_FINE_LOCATION ) != PackageManager .PERMISSION_GRANTED && ActivityCompat .checkSelfPermission (this , Manifest .permission .ACCESS_COARSE_LOCATION ) != PackageManager .PERMISSION_GRANTED ) {
153+ return ;
154+ }
155+ mLocManager .removeUpdates (this );
156+ }
157+
151158 @ Override
152159 public void onLocationChanged (@ NonNull Location location ) {
153160 lastLocationUpdate = System .currentTimeMillis ();
@@ -158,7 +165,7 @@ public void onLocationChanged(@NonNull Location location) {
158165 updateNotification (getString (R .string .meter , distanceToDestination ) + " / " + timeString , false );
159166 } else {
160167 updateNotification (null , true );
161- mLocManager . removeUpdates ( this );
168+ stopGpsLocListener ( );
162169 isWatchdogRunning = false ;
163170 handler .postDelayed (this ::stopSelf , 30000 );
164171 }
@@ -171,7 +178,7 @@ private void onLocationUpdateTimeout() {
171178 updateNotification ( timeString , false );
172179 } else {
173180 updateNotification (null , true );
174- mLocManager . removeUpdates ( this );
181+ stopGpsLocListener ( );
175182 isWatchdogRunning = false ;
176183 handler .postDelayed (this ::stopSelf , 30000 );
177184 }
@@ -199,7 +206,7 @@ private void updateNotification(@Nullable String contentText, boolean hasArrived
199206 @ Override
200207 public void onDestroy () {
201208 handler .removeCallbacks (watchdogRunnable );
202- mLocManager . removeUpdates ( this );
209+ stopGpsLocListener ( );
203210 super .onDestroy ();
204211 }
205212}
0 commit comments