Skip to content

Commit

Permalink
Fix crash when starting foreground service without location permission
Browse files Browse the repository at this point in the history
  • Loading branch information
platypii committed Dec 4, 2024
1 parent 8d952e0 commit 668e168
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions app/src/main/java/com/platypii/baseline/ForegroundService.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.platypii.baseline;

import com.platypii.baseline.util.Exceptions;

import android.app.Notification;
import android.app.Service;
import android.content.Intent;
Expand Down Expand Up @@ -58,12 +60,20 @@ public int onStartCommand(@Nullable Intent intent, int flags, int startId) {
private void updateNotification() {
if (logging || audible) {
// Show notification
if (!Permissions.hasLocationPermissions(this)) {
Log.w(TAG, "Cannot start foreground service without location permission");
return;
}
Log.i(TAG, "Showing notification");
final Notification notification = Notifications.getNotification(this, logging, audible);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
startForeground(Notifications.notificationId, notification, ServiceInfo.FOREGROUND_SERVICE_TYPE_LOCATION);
} else {
startForeground(Notifications.notificationId, notification);
try {
final Notification notification = Notifications.getNotification(this, logging, audible);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
startForeground(Notifications.notificationId, notification, ServiceInfo.FOREGROUND_SERVICE_TYPE_LOCATION);
} else {
startForeground(Notifications.notificationId, notification);
}
} catch (Exception e) {
Exceptions.report(e);
}
} else {
// Stop service
Expand Down

0 comments on commit 668e168

Please sign in to comment.