Show/Hide Toolbars

Android Applications

Navigation: Android Specifics > Android "Service"

Setting up Service Connection

Scroll Prev Top Next More

Host Application

As stated previously an "Android Service" is always packaged as part of a host application which manages or makes use of the service. That application will reference System.Android.Service library to interact with the service.

 

Attaching the Service

First build the service then in the host application select the Android target platform. On the Android target platform, right-click and select Add Android Service.

 

 

 

Starting The Service

The TLocalServiceConnection/TRemoteServiceConnection class functions

  StartService(const AServiceName: string);

  StartJobIntentService(const AServiceName: string; AJobID: Integer = 1; const AIntent: JIntent = nil);

can be used to launch a service that then controls its own destiny.

 

If you wish to interact with the service you create and hold an instance of a connection object and populate the call backs.

 OnConnected

 OnDisconnected

or use procedures

BindService

UnbindService

 

 

Call Backs (From TLocalServiceConnection)

 

ServiceConnected(const LocalService: TAndroidBaseService);

Called when the connection between the native activity and the service has been established. It is used to obtain the

binder object that allows the direct interaction between the native activity and the service.

A pointer to the Service Datamodule is made available in the call back.

It can be type cast using        FDMService := TSrvDM(ALocalService);

but I had trouble using        FDMService := ALocalService as TSrvDM;

 

ServiceDisconnected;

Called when the connection between the native activity and the service has been unexpectedly lost (e.g. when the user

manually stops the service using the 'Settings' application).

 

 

 

 

Wake lock permission

WAKE_LOCK

JStringToString

               (TJManifest_permission.JavaClass.ACCESS_FINE_LOCATION);

 

 

 

https://developer.android.com/training/scheduling/wakelock

 

PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);

WakeLock wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,

       "MyApp::MyWakelockTag");

wakeLock.acquire();

 

 

 

 

 

 

TAndroidHelper.Activity.bindService(LIntent, FConnection, AFlags);