Class ForegroundService
Runs a long lived task while a persistent system notification is shown to the user.
This is an Android foreground service. On iOS, which has no foreground service concept,
the task runs under a limited background execution window accompanied by a local
notification (best effort); check #isSupported() to detect full support.
Usage
ForegroundService svc = ForegroundService.start("downloads", "Downloading", "Please wait", null,
service -> {
for (int i = 0; i <= 100; i++) {
service.updateNotification("Downloading", i + "%");
// ... do a chunk of work ...
}
});
// the service auto-stops when the task returns, or call svc.stop() early
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceThe long running task executed by a foreground service. -
Method Summary
Modifier and TypeMethodDescriptionbooleanReturns true if the service is currently running.static booleanReturns true if the current platform fully supports foreground services.static ForegroundServiceStarts a foreground service that shows a persistent notification and runs the task on a background thread.voidstop()Stops the service and removes its notification.voidupdateNotification(String title, String body) Updates the text of the foreground service notification.
-
Method Details
-
start
public static ForegroundService start(String channelId, String title, String body, String iconName, ForegroundService.Task task) Starts a foreground service that shows a persistent notification and runs the task on a background thread.
Parameters
-
channelId: the notification channel id to post the notification on -
title: the notification title -
body: the notification body -
iconName: the small icon resource name, or null for the default app icon -
task: the work to run
Returns
the running service handle
-
-
updateNotification
-
stop
public void stop()Stops the service and removes its notification. -
isRunning
public boolean isRunning()Returns true if the service is currently running.
Returns
true if running
-
isSupported
public static boolean isSupported()Returns true if the current platform fully supports foreground services.
Returns
true if foreground services are supported
-