[ShareLocation] Fixed cancelation intent

Fixed the intent to cancel an ongoing sharing, which is now
delivered properly.
This commit is contained in:
Alexander Dörflinger
2025-03-25 16:24:16 +01:00
parent eab7153ea1
commit aecbaf91c8
4 changed files with 11 additions and 5 deletions

View File

@@ -3,6 +3,7 @@ package com.aldo.apps.familyhelpers;
import static com.aldo.apps.familyhelpers.utils.GlobalConstants.DEFAULT_HOME_LATITUDE;
import static com.aldo.apps.familyhelpers.utils.GlobalConstants.DEFAULT_HOME_LONGITUDE;
import static com.aldo.apps.familyhelpers.utils.GlobalConstants.METER_PER_SECOND_TO_KMH_CONVERTER;
import static com.aldo.apps.familyhelpers.utils.GlobalConstants.SHARE_LOCATION_CANCEL_ACTION;
import android.content.Intent;
import android.content.res.Configuration;
@@ -211,6 +212,7 @@ public class ShareLocationActivity extends AppCompatActivity implements OnMapRea
startForegroundService(shareLocationService);
} else {
final Intent stopLocationService = new Intent(this, ShareLocationBackgroundWorker.class);
stopLocationService.setAction(SHARE_LOCATION_CANCEL_ACTION);
stopService(stopLocationService);
}
});

View File

@@ -38,7 +38,7 @@ public final class GlobalConstants {
/**
* The action to be invoked when the sharing of location should be cancelled.
*/
public static final String SHARE_LOCATION_CANCEL_ACTION = "com.aldo.apps.familyhelpers.CANCEL_LOCATION_SHARING";
public static final String SHARE_LOCATION_CANCEL_ACTION = "SHARE_LOCATION_CANCEL";
/**
* The key of the extra to be applied to the starting intent of the sleepTimer service,
* holding the initial duration in millis.

View File

@@ -251,7 +251,7 @@ public final class LocationHelper implements LocationListener {
final Intent deleteIntent = new Intent(context, ShareLocationBackgroundWorker.class);
deleteIntent.setAction(SHARE_LOCATION_CANCEL_ACTION);
final PendingIntent pendingDeleteIntent = PendingIntent.getBroadcast(context, 0,
final PendingIntent pendingDeleteIntent = PendingIntent.getService(context, 0,
deleteIntent, PendingIntent.FLAG_IMMUTABLE);
return new NotificationCompat.Builder(context, SHARE_LOCATION_CHANNEL_ID)

View File

@@ -1,5 +1,7 @@
package com.aldo.apps.familyhelpers.workers;
import static com.aldo.apps.familyhelpers.utils.GlobalConstants.DEFAULT_MINIMUM_LOCATION_INTERVAL_METERS;
import static com.aldo.apps.familyhelpers.utils.GlobalConstants.DEFAULT_MINIMUM_LOCATION_INTERVAL_MILLIS;
import static com.aldo.apps.familyhelpers.utils.GlobalConstants.SHARE_LOCATION_CANCEL_ACTION;
import static com.aldo.apps.familyhelpers.utils.GlobalConstants.SLEEP_TIMER_NOTIFICATION_ID;
@@ -50,13 +52,14 @@ public class ShareLocationBackgroundWorker extends Service {
return START_REDELIVER_INTENT;
}
if (intent.getAction() != null && intent.getAction().equals(SHARE_LOCATION_CANCEL_ACTION)) {
stopSelf();
Log.d(TAG, "onStartCommand: Stopping service.");
mLocationHelper.stopLocationUpdates();
stopSelf();
return START_NOT_STICKY;
}
mShareLocationRunnable = () ->
mLocationHelper.startLocationUpdates(ShareLocationBackgroundWorker.this,
1000, 0);
DEFAULT_MINIMUM_LOCATION_INTERVAL_MILLIS, DEFAULT_MINIMUM_LOCATION_INTERVAL_METERS);
mHandler.post(mShareLocationRunnable);
startForeground(SLEEP_TIMER_NOTIFICATION_ID, mLocationHelper.buildNotification(this));
return super.onStartCommand(intent, flags, startId);
@@ -70,7 +73,8 @@ public class ShareLocationBackgroundWorker extends Service {
@Override
public void onDestroy() {
super.onDestroy();
mLocationHelper.stopLocationUpdates();
mHandler.removeCallbacks(mShareLocationRunnable);
super.onDestroy();
}
}