FMX.Remote Service Demo Sample
This demo shows two Android applications that use a remote Android service to present a message in the screen. The Android service works as a background task.
The service presents the text in the Android device, after the application binds to the service, and clicking Get Data. Data is only accessible while the application is bound to the service.
Location
You can find the Remote Service sample project at:
- Start | Programs | Embarcadero RAD Studio Alexandria | Samples, and then navigate to:
Object Pascal\Multi-Device Samples\Device Sensors and Services\RemoteServiceDemo
- Subversion Repository:
- You can find Delphi code samples in GitHub Repositories. Search by name into the samples repositories according to your RAD Studio version.
Description
This sample demonstrates how to use remote Android services from local and remote applications, to present a message in the screen. We understand local application as the application that contains the service, and remote application as the application that access a service hosted in a different application.
The Remote Service demo consists of one remote service and two applications, the app that contains the service and an external application:
- Remote Service: This is a remote Android service project. It consists of a Data Module without visual components, the functionality for this demo is included in the code.
- App Remote Host: This is a Multi-Device application that contains the service. The application provides three buttons:
- Bind: To bind to the service. After binding to the service, the Get Data button is enabled.
- UnBind: To unbind to the service. After unbinding from the service, the Get Data button is disabled.
- Get Data: To get data from the service hosted in the application.
- App Remote External: This is a Multi-Device application that uses a remote service connection to bind to the service included in the App Remote Host application. The application provides three buttons:
- Bind: To bind to the remote service. After binding to the service, the Get Data button is enabled.
- UnBind: To unbind to the remote service. After unbinding from the service, the Get Data button is disabled.
- Get Data: To get data from the remote service.
How to Use the Sample
In the AppRemoteHost
- Navigate to the location given above.
- Open the sample application group project file: RemoteDemo.groupproj.Note: The RemoteService is already attached to the App Remote Host application. The service comes by default selected in Project Dependencies, which means that you do not need to compile the project after it changes.
- In the Projects Window, select AppRemoteHost.
- Press F9 or choose Run > Run to deploy the application to an Android device.
- Click Bind to bind to the local service.Note: You can bind to the service from the application that hosts the service and by the external application.
- Click Get Data to show the message on the screen.Note: Get Data only appears enabled while the application is bound to the service.
- Click UnBind to unbind from the service.
In the AppRemoteExternal
- Navigate to the location given above.
- Open the sample application group project file: RemoteDemo.groupproj.Note: The RemoteService is already attached to the App Remote Host application. The service comes by default selected in Project Dependencies, which means that you do not need to compile the project after it changes.
- In the Projects Window, select AppRemoteExternal.
- Press F9 or choose Run > Run to deploy the application to the same Android device.
- Click Bind to bind to the local service.Note: You can bind to the service from the application that hosts the service and by the external application.
- Click Get Data to show the message on the screen.Note: Get Data only appears enabled while the application is bound to the service.
- Click UnBind to unbind from the service.
Implementation
Remote Service
The Remote Service is an Android service project. The type of the service is remote.
This option includes the following line in the Android Manifest file of the AppRemoteHost, after compiling:
<service android:exported="true" android:name="com.embarcadero.services.RemoteService" /> //RemoteService is the name of the service. Exported means that can be accessed by an external application.
The OnHandleMessage event of the Data Module triggers a handler instance that presents a string in the Android application.
Two codes are defined in the service:
GET_STRING = 123;
SERVICE_STRING = 321;
App Remote Host
AppRemoteHost is a Multi-Device application project that hosts the service.
A specific line included in the Android Manifest file allows external applications to use RemoteService.
<service android:exported="true" android:name="com.embarcadero.services.RemoteService" /> //RemoteService is the name of the service. Exported means that can be accessed by an external application.
- The OnCreate event from the TForm creates the remote service connection.
- The OnConnected event of the TRemoteServiceConnection is associated to the OnServiceConnected procedure.
- The OnHandleMessage event of the TRemoteServiceConnection is associated to the OnHandleMessage procedure.
Bind Button
It stablishes the connection to RemoteService with TRemoteServiceConnection.BindService('APackageName', AServiceName)
. If the service is not started, it also starts it.
procedure TForm1.Button3Click(Sender: TObject);
begin
FServiceConnection.BindService('com.embarcadero.AppRemoteHost',
'com.embarcadero.services.RemoteService');
end;
The OnConnected event fires when you are connected to the service.
The OnConnected calls the OnServiceConnected procedure with the Service Messenger (JMessenger), and enables the Get Data button.
UnBind Button
It unbinds from the service with TRemoteServiceConnection.UnbindService
. It does not stop the service when it is bound to other applications.
procedure TForm1.Button4Click(Sender: TObject);
begin
FServiceConnection.UnbindService;
end;
Get Data
Get Data establishes the connection with the service messenger. The local messenger and the service messenger establishes the interaction using a verification code GET_STRING = 123
.
procedure TForm1.Button2Click(Sender: TObject);
var
LMessage: JMessage;
const
GET_STRING = 123;
begin
LMessage := TJMessage.JavaClass.obtain(nil, GET_STRING);
LMessage.replyTo := FServiceConnection.LocalMessenger;
FServiceConnection.ServiceMessenger.send(LMessage);
end;
When the service receives the right code, replies sending the message.
The OnHandleMessage event fires when the message arrives to the local messenger. It handles the answer from the service using a verification code SERVICE_STRING = 321
.
The OnHandleMessage procedure presents the message from the service in the screen.
App Remote External
AppRemoteExternal is a Multi-Device application project that connects to RemoteService hosted in AppRemoteHost.
Bind Button
It stablishes the connection to RemoteService with TRemoteServiceConnection.BindService('APackageName', AServiceName)
. If the service is not started, it also starts it.
procedure TForm1.Button3Click(Sender: TObject);
begin
FServiceConnection.BindService('com.embarcadero.AppRemoteHost',
'com.embarcadero.services.RemoteService');
end;
The OnConnected event fires when you are connected to the service.
The OnConnected calls the OnServiceConnected procedure with the Service Messenger (JMessenger), and enables the Get Data button.
UnBind Button
It unbinds from the service with TRemoteServiceConnection.UnbindService
. It does not stop the service when it is bound to other applications.
procedure TForm1.Button4Click(Sender: TObject);
begin
FServiceConnection.UnbindService;
end;
Get Data
Get Data establishes the connection with the service messenger. The local messenger and the service messenger establishes the interaction using a verification code GET_STRING = 123
.
procedure TForm1.Button2Click(Sender: TObject);
var
LMessage: JMessage;
const
GET_STRING = 123;
begin
LMessage := TJMessage.JavaClass.obtain(nil, GET_STRING);
LMessage.replyTo := FServiceConnection.LocalMessenger;
FServiceConnection.ServiceMessenger.send(LMessage);
end;
When the service recives the right code, replies sending the message.
The OnHandleMessage event fires when the message arrives to the local messenger. It handles the answer from the service using a verification code SERVICE_STRING = 321
.
The OnHandleMessage procedure presents the message from the service in the screen.
Uses
- System.Android.Service
- System.Android.ServiceApplication
The sample uses Built-in Java libraries for Android.
- Androidapi.Helpers
- AndroidApi.JNI.GraphicsContentViewText
- Androidapi.JNI.Os
- Androidapi.JNI.JavaTypes
- Androidapi.JNI.Widget