Skip to content

Commit 668e168

Browse files
committed
Fix crash when starting foreground service without location permission
1 parent 8d952e0 commit 668e168

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

app/src/main/java/com/platypii/baseline/ForegroundService.java

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.platypii.baseline;
22

3+
import com.platypii.baseline.util.Exceptions;
4+
35
import android.app.Notification;
46
import android.app.Service;
57
import android.content.Intent;
@@ -58,12 +60,20 @@ public int onStartCommand(@Nullable Intent intent, int flags, int startId) {
5860
private void updateNotification() {
5961
if (logging || audible) {
6062
// Show notification
63+
if (!Permissions.hasLocationPermissions(this)) {
64+
Log.w(TAG, "Cannot start foreground service without location permission");
65+
return;
66+
}
6167
Log.i(TAG, "Showing notification");
62-
final Notification notification = Notifications.getNotification(this, logging, audible);
63-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
64-
startForeground(Notifications.notificationId, notification, ServiceInfo.FOREGROUND_SERVICE_TYPE_LOCATION);
65-
} else {
66-
startForeground(Notifications.notificationId, notification);
68+
try {
69+
final Notification notification = Notifications.getNotification(this, logging, audible);
70+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
71+
startForeground(Notifications.notificationId, notification, ServiceInfo.FOREGROUND_SERVICE_TYPE_LOCATION);
72+
} else {
73+
startForeground(Notifications.notificationId, notification);
74+
}
75+
} catch (Exception e) {
76+
Exceptions.report(e);
6777
}
6878
} else {
6979
// Stop service

0 commit comments

Comments
 (0)