Show/Hide Toolbars

Android Applications

Navigation: Android Specifics

Android "Service"

Scroll Prev Top Next More

An "Android Service" is an application without a user interface that performs background tasks. In Delphi you create the libMyCustomService.o service project file by choosing File>New>Other>Android Service. You then need to choose from

Local Service - Only available to Native Host Application. DM can be bound to Native Host Application at connect time making contents available  

Intent Local Service

Remote Service - Can be accessed by any Application by referencing Package and Service. Host Application need not be running - only needs to be "Installed".

Intent Remote Service

If you name the project file "MyCustomService" it shows in the project window as "libMyCustomService.so".

Save each Android Service project in an exclusive folder.

Do not name the Android Service project as Service.

 

The choice made above will create a default data module derived from TAndroidService or TAndroidIntentService. The Local/Remote choice will determine the type of connection you need to use  (TLocalServiceConnection or TRemoteServiceConnection) in your host application and the <service> entry that will eventually be inserted in the host application Manifest file.

 

Android services cannot exist in isolation they need to be packaged with a "Host" application which manages the service. A typical FireMonkey application for Android generally consists of a single activity, which is the  native "Host" activity. These application can be associated with one or more  Android Services (lib*.so) files in the project manager. The native activity then needs to either start or bind to the services at run time.

 

Sometimes the service  needs to run in the foreground when the native activity not visible so it is not affected by the background location limits introduced as part of Android 8.0. Running a service in the foreground requires an ongoing notification to be present to the user in order to indicate that the application is actively running.

 

There are essentially two types of services:

A started service: This service is started by an Android application. The service can run in the background indefinitely, even if the application is closed. This type of service usually performs a single task and automatically stops after finishing.

A bound service: This service only runs while it is bound to an Android application. There is an interaction between the application and the service, and it remains active until the application unbinds. More than one application can bind to the same (???remote??) service.

Unbinding the native activity from the service ensures that the native activity is no longer a bound client and the service can be destroyed by the system if there is no other bound client. The native activity is the only bound client used in Foreground Background demo application. If the service is also a started service, the system will destroy the service only after a call to the 'stopSelf' procedure.

 

Warning

FMX.Types

Services Libraries (projects) cannot call FMX.Types, FMX.Objects or FMX .Graphics

 

 

 

https://docwiki.embarcadero.com/RADStudio/Berlin/en/Creating_Android_Services