java.lang.Object | |
↳ | com.neomades.app.Service |
A Service allows an Application to execute code in the background outside the foreground application even when the application is not running.
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.
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
.
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.
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.
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:
startService(Class)
: onExecute()
will be
immediately executed oncescheduleService(Class)
: onExecute()
will be
executed immediately and then every 30 minutesscheduleService(Class, long)
: onExecute()
will be executed immediately and then every x minutes, where x is a time in
minutes
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.
Each service class must have a corresponding <service> declaration in its project's URS file.
<service name="your.service.name" />
A sample project about using Service
in NeoMAD is available in
the NeoMAD installation directory under Examples/ServiceExample
.
In Android
a Service
is
androidx.work.ListenableWorker
.
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.
In iOS
, the application declares the "fetch" mode in order to be
able to execute Service in background.
onExecute()
calls: 1 minuteConstants | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
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 | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
![]() |
Scheduled service interval: launch the service every 30 minutes
Scheduled service interval: launch the service every day
Scheduled service interval: launch the service every hour
Scheduled service interval: launch the service every minute
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.
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.