public abstract class

Service

extends Object
java.lang.Object
   ↳ com.neomades.app.Service

Class Overview

A Service allows an Application to execute code in the background outside the foreground application even when the application is not running.

About services

A Service is an application component representing an application's desire to perform a longer-running operation while not interacting with the user. Even if a service is linked with an application, it will have its own life, independently from the application. Consequently, it will continue to execute even if the application is not currently running.

NB. Time between service calls, time available to execute the service and the execution mechanism of service in background are strongly linked to the platform where the Service runs. Read carefully the Cross Platform considerations section.

When to execute

The service can execute a piece of code immediately ( startService(Class)) or periodically ( scheduleService(Class) or scheduleService(Class, long)). Since only one instance of the same Service can be active at the same time, if a Service is started or scheduled several time, only the last call will be considered. To limit the impact of the service on the energy and memory consumption of the device, be careful to use long intervals between each service call. Some platforms have limits with regards to periodicity of a service execution. Read the Cross Platform considerations section for more details about this and prefer using the NeoMAD constants INTERVAL_MINUTE, INTERVAL_30_MINUTES, INTERVAL_HOUR, INTERVAL_DAY.

What to execute

Remember that a service is independent from its application. This means that it won't be able to directly access elements of the application (instance of Application cannot be created from services). Consequently, a service is only authorized to execute non-UI code : http connections, save or load preferences, access to file system, show or schedule notifications.

If information really needs to be transfered between an application and its service, Preferences or File may be used. However, be careful when synchronizing between the application and the service to avoid them accessing files or preferences at the same time and cause unstable states.

How the service works

To use a service in an application, the service to create must override this Service abstract class and implement its onExecute() method. onExecute() must contain the code to run when the service is activated. This method is called in the main application thread. You must create your own thread if you do not want to block the UI-thread.

Be sure to call serviceFinished() after all the tasks of your service have finished their works else the current service execution won't stop and it will not be scheduled again.

Services can be created using different ways:

Scheduled services can be stopped at any moment by calling stopService(Class). To start them again, a call to startService(Class) or scheduleService(Class, long) is required.

NB. In some platforms the time given by the system to run a service is limited. This means that the service will be killed if it takes too long to execute. Please refer to the Cross Platform considerations section for more detail about this.

NeoMAD urs

Each service class must have a corresponding <service> declaration in its project's URS file.

 <service name="your.service.name" />
 

NeoMAD sample

A sample project about using Service in NeoMAD is available in the NeoMAD installation directory under Examples/ServiceExample.

Cross Platform Considerations

In Android a Service is androidx.work.ListenableWorker.

  • Minimum time between two onExecute() calls: at least 15 minutes but depends on the device usage. The system choose when to execute a service according to the available resources. When a schedule interval is defined, the system will try to respect it but the result may be very different from the one expected.
  • Maximum service execution time: no limit
  • Service will be stopped when the application is in background: no

In iOS, the application declares the "fetch" mode in order to be able to execute Service in background.

  • Minimum time between two onExecute() calls: 1 minute
  • Maximum service execution time: no limit if the application is in foreground, 30s if the application is in background
  • Service will be stopped when the application is in background: no however it is the iOS system that will decide when the service fires, whatever is the schedule time. Apple uses its own algorithm to determine when the service can execute. It depends on the time and resources the service execution requires, on the frequency the application is used, etc.
  • Service will be stopped when the application is killed.

Summary

Constants
long INTERVAL_30_MINUTES Scheduled service interval: launch the service every 30 minutes
long INTERVAL_DAY Scheduled service interval: launch the service every day
long INTERVAL_HOUR Scheduled service interval: launch the service every hour
long INTERVAL_MINUTE Scheduled service interval: launch the service every minute
Public Constructors
Service()
Public Methods
final void serviceFinished()
Informs the Controller that the service execution is over.
Protected Methods
abstract void onExecute()
This method is called when the service is executed.
[Expand]
Inherited Methods
From class java.lang.Object

Constants

public static final long INTERVAL_30_MINUTES

Scheduled service interval: launch the service every 30 minutes

Constant Value: 1800000 (0x00000000001b7740)

public static final long INTERVAL_DAY

Scheduled service interval: launch the service every day

Constant Value: 86400000 (0x0000000005265c00)

public static final long INTERVAL_HOUR

Scheduled service interval: launch the service every hour

Constant Value: 3600000 (0x000000000036ee80)

public static final long INTERVAL_MINUTE

Scheduled service interval: launch the service every minute

Constant Value: 60000 (0x000000000000ea60)

Public Constructors

public Service ()

Public Methods

public final void serviceFinished ()

Informs the Controller that the service execution is over. This is useful to help the system to know that it can release the wakelock being held for the service.

This will not prevent a periodic service to do its next execution. It is only a way to indicate that the current execution is finished.

Protected Methods

protected abstract void onExecute ()

This method is called when the service is executed. Override this method with the code to execute in the service. This method is called in the main application thread. You must create your own thread if you do not want to block the UI-thread.

Be sure to call serviceFinished() after all the tasks of your service have finished their works.