Start NotificationCollectorMonitorService as foreground service

This commit is contained in:
Daniele Gobbetti
2025-07-07 08:37:13 +02:00
parent c9812aac54
commit 99a05742ff
3 changed files with 30 additions and 2 deletions
@@ -49,6 +49,7 @@ import android.util.TypedValue;
import androidx.annotation.VisibleForTesting;
import androidx.core.app.NotificationCompat;
import androidx.core.content.ContextCompat;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import java.io.File;
@@ -333,7 +334,11 @@ public class GBApplication extends Application {
}
try {
//the following will ensure the notification manager is kept alive
startService(new Intent(this, NotificationCollectorMonitorService.class));
Intent serviceIntent = new Intent(context, NotificationCollectorMonitorService.class);
//ContextCompat starts a background service on android < O automatically despite the method name
ContextCompat.startForegroundService(context, serviceIntent);
} catch (IllegalStateException e) {
String message = e.toString();
final Intent instructionsIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://gadgetbridge.org/basics/topics/background-service/"));
@@ -17,6 +17,7 @@
package nodomain.freeyourgadget.gadgetbridge.service;
import android.app.ActivityManager;
import android.app.Notification;
import android.app.Service;
import android.content.ComponentName;
import android.content.Context;
@@ -25,13 +26,19 @@ import android.content.pm.PackageManager;
import android.os.IBinder;
import android.os.Process;
import androidx.core.app.NotificationCompat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.List;
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
import nodomain.freeyourgadget.gadgetbridge.R;
import nodomain.freeyourgadget.gadgetbridge.externalevents.NotificationListener;
import static nodomain.freeyourgadget.gadgetbridge.util.GB.NOTIFICATION_CHANNEL_ID;
/**
* Original source by xinghui - see https://gist.github.com/xinghui/b2ddd8cffe55c4b62f5d8846d5545bf9
* NB: no license specified in the source code as of 2017-04-19
@@ -39,16 +46,30 @@ import nodomain.freeyourgadget.gadgetbridge.externalevents.NotificationListener;
public class NotificationCollectorMonitorService extends Service {
private static final Logger LOG = LoggerFactory.getLogger(NotificationCollectorMonitorService.class);
private static final int ONGOING_NOTIFICATION_ID = 1;
@Override
public void onCreate() {
super.onCreate();
ensureCollectorRunning();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (GBApplication.isRunningOreoOrLater()) {
Notification notification = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
.setContentTitle(getString(R.string.service_notification_collector_service_title))
.setContentText(getString(R.string.service_notification_collector_service_text))
.setSmallIcon(R.drawable.ic_notification)
.setPriority(NotificationCompat.PRIORITY_LOW)
.build();
startForeground(ONGOING_NOTIFICATION_ID, notification);
}
ensureCollectorRunning();
return START_STICKY;
}
+2
View File
@@ -4107,4 +4107,6 @@
<string name="activity_print_image_apply_dithering_switch">Apply Dithering</string>
<string name="activity_print_image_upscale_switch">Upscale image</string>
<string name="activity_print_label_print_size">Print size (px): %1$d x %2$d</string>
<string name="service_notification_collector_service_title">Gadgetbridge Notification Monitor</string>
<string name="service_notification_collector_service_text">Monitoring notifications in background</string>
</resources>