2020package de .grobox .transportr ;
2121
2222import android .Manifest ;
23+ import android .annotation .SuppressLint ;
2324import android .app .PendingIntent ;
2425import android .app .Service ;
2526import static androidx .core .app .NotificationManagerCompat .IMPORTANCE_MAX ;
@@ -148,6 +149,14 @@ private void startGpsLocListener() {
148149 mLocManager .requestLocationUpdates (LocationManager .GPS_PROVIDER , LOCATION_INTERVAL_MS , 0 , this );
149150 }
150151
152+ @ SuppressLint ("MissingPermission" )
153+ private void stopGpsLocListener () {
154+ if (ActivityCompat .checkSelfPermission (this , Manifest .permission .ACCESS_FINE_LOCATION ) != PackageManager .PERMISSION_GRANTED && ActivityCompat .checkSelfPermission (this , Manifest .permission .ACCESS_COARSE_LOCATION ) != PackageManager .PERMISSION_GRANTED ) {
155+ return ;
156+ }
157+ mLocManager .removeUpdates (this );
158+ }
159+
151160 @ Override
152161 public void onLocationChanged (@ NonNull Location location ) {
153162 lastLocationUpdate = System .currentTimeMillis ();
@@ -158,7 +167,7 @@ public void onLocationChanged(@NonNull Location location) {
158167 updateNotification (getString (R .string .meter , distanceToDestination ) + " / " + timeString , false );
159168 } else {
160169 updateNotification (null , true );
161- mLocManager . removeUpdates ( this );
170+ stopGpsLocListener ( );
162171 isWatchdogRunning = false ;
163172 handler .postDelayed (this ::stopSelf , 30000 );
164173 }
@@ -171,7 +180,7 @@ private void onLocationUpdateTimeout() {
171180 updateNotification ( timeString , false );
172181 } else {
173182 updateNotification (null , true );
174- mLocManager . removeUpdates ( this );
183+ stopGpsLocListener ( );
175184 isWatchdogRunning = false ;
176185 handler .postDelayed (this ::stopSelf , 30000 );
177186 }
@@ -199,7 +208,7 @@ private void updateNotification(@Nullable String contentText, boolean hasArrived
199208 @ Override
200209 public void onDestroy () {
201210 handler .removeCallbacks (watchdogRunnable );
202- mLocManager . removeUpdates ( this );
211+ stopGpsLocListener ( );
203212 super .onDestroy ();
204213 }
205214}
0 commit comments