# Class ForegroundService

java.lang.Object
com.codename1.background.ForegroundService

public final class ForegroundService extends java.lang.Object

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 Classes
  Modifier and Type
  Class
  Description
  `static interface`
  `ForegroundService.Task`
  The long running task executed by a foreground service.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `boolean`
  `isRunning()`
  Returns true if the service is currently running.
  `static boolean`
  `isSupported()`
  Returns true if the current platform fully supports foreground services.
  `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.
  `void`
  `stop()`
  Stops the service and removes its notification.
  `void`
  `updateNotification(String title, String body)`
  Updates the text of the foreground service notification.

  ### Methods inherited from class java.lang.Object

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- ## 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

    public void updateNotification(String title, String body)

    Updates the text of the foreground service notification.

    ###### Parameters

    - `title`: the new title

    - `body`: the new body

  - ### 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